Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regd. constructor chaining

 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Ranchers,
when i try to run the following code its gives the output as

Mobile.showDevice,null
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device




but i am expecting the output as
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device
(bcas first it will inializes the instance variables then the constructor will run)

Thanks in advance.
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bala,

I will try to put my explanation for this in one liner, think about that.
" Instance variables gets initialized after the call to the super() is finished its execution."

Regards,
Mausam
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the code u have,
after the line Mobile n =new Mobile();
the control goes to super class constructor ,and there when u call showDevice,
concept of shadowing takes place i.e it refers to sub class showDevice() method,at this time the var device is not initialized still,hence null.
and the rest ouput as the flow.

if u put static modifier to the String device u wont get null,but Mobile Device try out.
hope I am right
 
Mausam M Kakkad
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I assume Bala has understood the point. Is it??

Regards,
Mausam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic