wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instance variable initiating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Instance variable initiating" Watch "Instance variable initiating" New topic
Author

Instance variable initiating

dilan alex
Greenhorn

Joined: Sep 17, 2011
Posts: 27
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

Joined: Jan 26, 2011
Posts: 33
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

Joined: Sep 17, 2011
Posts: 27
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
Sheriff

Joined: Sep 28, 2004
Posts: 18116
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
dilan alex
Greenhorn

Joined: Sep 17, 2011
Posts: 27
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

Joined: Oct 23, 2011
Posts: 384

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 !!


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance variable initiating
 
Similar Threads
static methods and NULL
Nested Class - Issue
Inheritance-1
Operators and Precedence
instanceof operator