Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance variable initiating

 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,



The result of the above code is 'null'. I cannot understand why it results a null.
Can anyone explain >>

Regards,
Dil.
 
Rumesh Eranga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not good at this much. I can't figure it out. But if you didn't over ride the method1 at BB class it would print "a".

regards.
 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rumesh Eranga wrote:I am not good at this much. I can't figure it out. But if you didn't over ride the method1 at BB class it would print "a".

regards.


I could figure out it.

Instance variables are instantiated after all of its constructors are completed. Here I create new BB object
'BB b = new BB();' It calls BB's default constructor and then AA's no-arg constructor. Within AA constructor it calls another method 'method1' which is in BB class.
At this time AA's constructor completes and assigns values to the 'a' and 'b'(in AA class). Since constructor of AA calls a method in BB, constructor of BB is not completed. So there are no any value for 'a' in class BB.
Since this is a String it has its default value null.

I think I'm correct.

Regards,
Dil.
 
Henry Wong
author
Marshal
Pie
Posts: 20991
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dilan alex wrote:


The result of the above code is 'null'. I cannot understand why it results a null.
Can anyone explain >>



The order of instantiation is... The super() part of the constructor is executed. The instance variables (and instance initializers) are initialized in order that they were defined in the class. And finally, the rest of the constructor is executed.

This means.... the instance variable for the BB class has not been instantiated at the time the constructor for AA is called.

So.... when the AA constructor calls the method1() method, which due to polymorphism, calls the method1() method of the BB class, the instance variables of the BB class has not been initialized yet.

Henry
 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have made a mistake with above post. It should be correct like this.



instance variables are assigned values after the call to super() in a constructor, in other words, after all
super-constructors have run.

Regards,
Dil.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More Clearly in Super and Sub class relationship :

Say constructor of Child is called
Then
constructor of Super class is called by Child class constructor.
Instance member are initialized , in specified order.
constructor of Super class completes.
Then
Instance member of Sub class gets initialized, in specified order. .
constructor of Sub class completes.

Both Super class and Sub class are Happy !!


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic