Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please tell me how this code works.

 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you print any object, the JVM calls its toString method, as you dont override it , it simply returns its default code ,

like this :

 
darshan karle
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic