• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Instance variable initiating

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!


 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic