This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes please tell me how this code works. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please tell me how this code works." Watch "please tell me how this code works." New topic
Author

please tell me how this code works.

Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

i dont get the reason, why is it printing "Test@19821f" in print statement //2 and //3.

class Test
{
int i;
}

class A
{
Test t;

A()
{
System.out.println(t); //1 null
}
}

class B extends A
{
B(Test t)
{
this.t = t;
System.out.println(t); //2 Test@19821f
System.out.println(this.t); //3 Test@19821f
}
}

class Inherit
{
public static void main(String punnu[])
{
Test t = new Test();
B b1 = new B(t);
}
}
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Whenever you print any object, the JVM calls its toString method, as you dont override it , it simply returns its default code ,

like this :



[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
darshan karle
Greenhorn

Joined: May 09, 2008
Posts: 28
hi puneet,

when you say print(t) it is same as print(t.toString()).
toString() is a method from Object class.If you do not add this toString()
compiler will do that for you.
Implementation given in the Object class toString method is given below



that's why it is printing like that.

for String object it print the value because in String class they have over ridden the Object class toString() method and they have given their own implementation.

regards
darshan
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

thanks for the reply friends, but what i am not getting is that in main when i am creating



i am passing t as an argument.Now this t is of derived type so it should have its default value as null, and when i m passing this null value to
constructor


it will assign this null value to this.t and then in both the print statements it should have printed null. but it is printing different thing so i want to ask that why is the default value of t null is not getting passed to B(Test t).
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
I'm not sure what you mean by "t has a default value of null". It gets assigned an object reference in the preceding line ("Test t = new Test()") so it should not be null.


Ping & DNS - my free Android networking tools app
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Now this t is of derived type so it should have its default value as null, and when i m passing this null value to
constructor


Why do you think t should be null when you clearly instantiate it before you pass it to the constructor of B?



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

thanks friend. so you mean to say that when we create a new object with some reference than that reference doesnt have any value. but then why is it printing null in first print statement.
.
.
.
.
.
.
.
i am totally confused in this question.please explain it clearly....
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1



Here reference "t" is not assigned to any object, so it simply points to "null" object ,, that's why you get "null" object in first line !!

HTH,
[ July 09, 2008: Message edited by: Sagar Rohankar ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The Test reference that is declared as a instance variable in A has nothing to do with the Test reference that is instantiated in your main method. They just happen to have the same name. So when the constructor for A runs, the Test reference that is an instance variable in a is null, but then you pas in a valid, initialized test reference, and you assign the instance Test reference to it. So now both Test references happen to have the same name 't' and also point to the same object.
 
 
subject: please tell me how this code works.
 
Similar Threads
Printing anything and Everything using varargs
Restoring an object in finalize() ?
Question on syncrhonized methods
how to make equal objects?
this(),super()