File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor question" Watch "constructor question" New topic
Author

constructor question

Ji-Hoon Jung
Greenhorn

Joined: Jun 18, 2010
Posts: 25
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Did you check the API of the println method ?


[My Blog]
All roads lead to JavaRanch
Ji-Hoon Jung
Greenhorn

Joined: Jun 18, 2010
Posts: 25
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 18, 2010
Posts: 25
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

Joined: Jun 18, 2010
Posts: 25
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jun 18, 2010
Posts: 25
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jun 18, 2010
Posts: 25
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

Joined: Nov 27, 2003
Posts: 62


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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jun 18, 2010
Posts: 25
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

Joined: Jun 18, 2010
Posts: 25
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

Joined: Jan 28, 2008
Posts: 5575

tremendous effort from Christophe Verré
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor question