I could not understand the Given code.... Any one there to explain that?


#1

Inside main method:
i.e

public static void main(String[] args){
File data = new File(55);
//Created a new object here

}

My question is
Why we have to put the integer in between parenthesis. As when we run this code, in console no 55 number is being displayed. So, why we have to keep this anymore?

If i removed 55 from parenthesis, i will get error on my code.

Anyone out there who can give some time to explain about it???


#2

I don’t think there’s a constructor for File that accepts an int, and I certainly wouldn’t expect 55 to be printed without using System.out.println or similar

So, for starters, what makes you say that you have to, or even can?
Perhaps you’re not using java.io.File, if so, then which class is File?
By “given code” do you mean something provided by codecademy? In which exercise?


#3

Thanks for the reply.
Yeah the class name is File
And yeah it is from codeacademy.
Link: https://www.codecademy.com/courses/learn-java/lessons/object-oriented-programming/exercises/objects?action=lesson_resume&link_content_target=interstitial_undefined

here I replace car class with file and myFastcar with data.


#4

If you renamed your class to File then it probably didn’t run at all, when codecademy runs your code it’ll run main in the Dog class, so most likely you were running a class that you had compiled earlier.

.
├── Dog.class  <- the main method here is the entry point
├── Dog.java   <- your code is in here
└── File.class <- you were creating this, which gets ignored

0 directories, 3 files

As for the integer, the constructor accepts an int, so to create an instance you have to provide an int


#5

Thanks again.
So to clear that one!!

Here is the overall Code!!!

public class Codeacademy{
int Cardate;

public Codeacademy(int date) {
	date = Cardate;
}
		public void desgined(int date) {
		System.out.println("The car was designed in: " + date);
	}
	
	public static void main(String[] args) {
		Codeacademy inout = new Codeacademy(465);
		inout.desgined(1888);
		
	}
}

Here, the class name is Codeacademy.

Look at line 12. In Codeacademy(465), if we don’t write 465 the code will get an error. But if we write the number 465 in Codeacademy() as given in above. Everything is correct. So, what I am trying to say is that, after I run that code given above. The number 465 isn’t displayed in the console at all. So, why we should need to write 465 in Codeacademy(). What is the uses of 465 number in Codeacademy??

Thank you.


#6

Not… entirely sure why you changed the class name again, but I guess you’re not asking about that anyway.

You do have two numbers and you make both of them available to your desgined method. If you’re only going to print one number, then yes, you probably shouldn’t give it two numbers, and you should probably be removing one of them and change desgined and/or the constructor so that it doesn’t accept a number at all

It’s a bit like the simpler situation of:

void show(int a, int b) {
    System.out.println(b);
}

Where a gets completely ignored. Just remove it.

void show(int b) {
    System.out.println(b);
}

Or perhaps both should be used instead?

void show(int a, int b) {
    System.out.println(a);
    System.out.println(b);
}

If you make an object that knows something about itself, then, when you ask it to tell you that about itself, you probably shouldn’t tell it that information again (no point in asking if you already know too)

bob = new Person(2001);
bob.displayAge();  // no need to tell bob how old they are, why would one want to?

#7

Thanks for giving some time!! Thank you


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.