File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regd. constructor chaining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regd. constructor chaining" Watch "Regd. constructor chaining" New topic
Author

Regd. constructor chaining

Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
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.


Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
Mausam M Kakkad
Ranch Hand

Joined: Oct 27, 2005
Posts: 40
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

Joined: Sep 12, 2005
Posts: 158
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


Harish<br />SCJP 1.4 (85%)
Mausam M Kakkad
Ranch Hand

Joined: Oct 27, 2005
Posts: 40
Hi,

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

Regards,
Mausam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regd. constructor chaining