permaculture playing cards*
The moose likes Beginning Java and the fly likes Problem Using toString method from a different class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem Using toString method from a different class" Watch "Problem Using toString method from a different class" New topic
Author

Problem Using toString method from a different class

Tom Brady
Greenhorn

Joined: Jan 11, 2005
Posts: 3
Hi,

I can not get the toString method to work from another class.





My problem is with the System.out.println(myobject);

If I leave it this way it displays. Number 1: 0 Number 2: 0

If I change the toSting method to accept the parameters like so..



Then the program will print out the name of the class with some garbage after it MyClass1@fabe9. What am I doing wrong?

The desired output is:

"Number 1: 3456789 Number 2: 150000."

Thanks a lot in advance for any advice.
[ January 11, 2005: Message edited by: Tom Brady ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


is calling the toString() method defined in the Object class. But:

is calling a toString method which takes two doubles as parameters (i.e. different from that defined in the Object class). Now when you write a piece of code like this:

What goes on in the background when you run this is the process looks for the toString() method for that object and calls it. This bit of logic is implicit and can't be changed. In order to get your class's toString method be called instead you need to make sure you override the Object class's one. So your toString method must take no parameters.
[ January 11, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Hi

1. Are you initializing the num1 and num2 class variable with with num1,num2 the constructor parameters in constructor code. you didn't mention that.

2.
Then the program will print out the name of the class with some garbage after it MyClass1@fabe9. What am I doing wrong?

This is happening because the statement - System.out.println(myobject) will call the myobject 's toString() and not toString(long,double). That is the default feature, so the latter method is never being called rather the toString() of Object class( i.e. the super class) is called, which prints the reference of myobject.

The only problem I see in your code, is step 1 above.
Tom Brady
Greenhorn

Joined: Jan 11, 2005
Posts: 3
Hi All,

Thanks for your help.

Paul: An important thing for me to learn here was that I needed to leave the toString() method without any parameters because yes, I wanted to Override the toString from the Object class.

Damanjit:

How come I had to do that to get toString to see the values of the double and long number? Where I had // Some More Code Here... I had some if statements that threw an Exception if the numbers were too high or too low. And those Exceptions printed out an error message with the values of the numbers. How come toString couldnt access those values until I made new ones using this.num1 = num1; ?
[ January 11, 2005: Message edited by: Tom Brady ]
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
How come I had to do that to get toString to see the values of the double and long number? Where I had // Some More Code Here... I had some if statements that threw an Exception if the numbers were too high or too low. And those Exceptions printed out an error message with the values of the numbers. How come toString couldnt access those values until I made new ones using this.num1 = num1; ?


1. public class MyClass1 {

private long num1;
private double num2;
....

Here num1 and num2 are class member variables thus initialized autmatically to default value 0 when you create an instance of MyClass1 in MyClass2 main method. At that time you write following statement :

2. MyClass1 myobject = new MyClass1(3456789, 150000);

to invoke constructor of MyClass1 passing values for num1 and num2 parameters described in constructor below.

3. public MyClass1 (long num1, double num2) throws OneException, AnotherException {

// Some Code Here...
}

The variables - num1 and num2 in constructor -MyClass1 are different from the class member variables num1 and num2

so at step 2 the value of num1 and num2 is 0 due to reasons mentioned in step 1 and value of num1 and num2 in constructor parameter is 3456789, 150000

4. this.num1 = num1;
this.num2 = num2;

this.num1 and this.num2 refer to class member variable and num1 , num2 to constructor parameters.
The rule is if you have variable with same name declared in a method (either in its body or parameter) as those class member varaibles, then to distinguish between these two variables you must use this for class member variables.

5. toString() is refering to class member varaibles which will have value 0 if you don't perform the step4.
Tom Brady
Greenhorn

Joined: Jan 11, 2005
Posts: 3
OOOOOOooooooooohhhhhhh

Now I get it! Duh! Hehehe

That was really a great explaination taking me through the process. I was thinking that the parameters I was passing to the class automatically filled into num1 and num2. Now I totally understand that when created num1 and num2 they werent assigned a value and that if I want to assign them the value of the parameters then I have to do it that way using this.num1 = num1;

Thanks a lot!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem Using toString method from a different class