Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor question

 
Ji-Hoon Jung
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the question:



I don't understand why would the "test" would print out, why would the toString function being runned? Thanks buddies!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the API of the println method ?
 
Ji-Hoon Jung
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Did you check the API of the println method ?


I just don't get why would the toString function being run since it looks more like a declaration of a constructor but not running it.....
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it looks more like a declaration of a constructor

A constructor ? Which class's ? Constructors have the same name as their class. Are you saying that doString looks like a constructor ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you to go through the source code of println method .

and by the way : in your example you are overriding the toString method in annonymous class

hth
 
Ji-Hoon Jung
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
it looks more like a declaration of a constructor

A constructor ? Which class's ? Constructors have the same name as their class. Are you saying that doString looks like a constructor ?


ok then just implement of the interface, but why the toString function would run?
 
Ji-Hoon Jung
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:I would suggest you to go through the source code of println method .

and by the way : in your example you are overriding the toString method in annonymous class

hth


cool so I think annonymous class means the class is not formall declarated but declare the way like this one, but why the toString function would run???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why the toString function would run???

Did you check the API ??? new TestA() creates a new instance of class TestA, which is passed to the println method. Now, what does println do ? Check the API.
 
Ji-Hoon Jung
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
but why the toString function would run???

Did you check the API ??? new TestA() creates a new instance of class TestA, which is passed to the println method. Now, what does println do ? Check the API.


So is that the class TestA is being declarated and then creates a new instance at the same time? So does the ToString function?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. A new instance (anonymous) of TestA is created. It implements toString, which actually overrides the Object class toString method.
2. println is called, passing it an Object of type TestA
3. The API explains what happens from there.
 
Ji-Hoon Jung
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:1. A new instance (anonymous) of TestA is created. It implements toString, which actually overrides the Object class toString method.
2. println is called, passing it an Object of type TestA
3. The API explains what happens from there.


How can I check API and get something from there? API is like long manual.....
 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Check the above code, the toString1() method is not called. When you try printing an object the toString() of the particular class is called, if there is no toString method then the toString() of the Object class is called if there is no other toString() implementation in that object's hierarchy
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I check API and get something from there?

If you don't learn to use the API, you'll often find yourself in troubles. Follow the following step :

1. Check the java.lang.System class.
2. Look for the "out" field. You'll see that it is a PrintStream. Click PrintStream.
3. Look for the println method. There are many overloaded println methods. In your case, you are passing an Object to it, so click the println(Object x) method.
It says :

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().


4. Now check the String.valueOf method, in the String class.
It says :
Returns the string representation of the Object argument.
Returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
 
Ji-Hoon Jung
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh Nagaraju wrote:

Check the above code, the toString1() method is not called. When you try printing an object the toString() of the particular class is called, if there is no toString method then the toString() of the Object class is called if there is no other toString() implementation in that object's hierarchy


cool so basically the println is calling the toString funtion of the anonymous class...
 
Ji-Hoon Jung
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
How can I check API and get something from there?

If you don't learn to use the API, you'll often find yourself in troubles. Follow the following step :

1. Check the java.lang.System class.
2. Look for the "out" field. You'll see that it is a PrintStream. Click PrintStream.
3. Look for the println method. There are many overloaded println methods. In your case, you are passing an Object to it, so click the println(Object x) method.
It says :

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().


4. Now check the String.valueOf method, in the String class.
It says :
Returns the string representation of the Object argument.
Returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.


ho I see so actually toString has special meaning for println, so amazing ha ha! Thanks!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tremendous effort from Christophe Verré
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic