jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When the class will be loaded??? 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 "When the class will be loaded???" Watch "When the class will be loaded???" New topic
Author

When the class will be loaded???

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
This is from Devaka's Exam. But not sure because I copied this in my local disk....

OUTPUT :Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device

When the class Phone will be loaded to get the static variable device initialized???
Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
I think the static variable device is being correctly initialized.

The thing is that the method showDevice() of the Phone class is never run. But I'm not totally sure why.


Rafael Angarita.
SCJP 6.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

when you enter the command
java Mobile

JVM trys to load Mobile class, at that time it notices that the class Mobile has extended Phone class.So it will load Phone class .
Then it will see that Phone class has a static member .So it will assign the valuue for the static member and then it will load the class Mobile.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Rafael Angarita
Ranch Hand

Joined: Jan 09, 2009
Posts: 67
The Phone constructor calls the overridden version of showDevice() before the class Mobile gets initialized.

But why the Phone constructor calls the overridden version of showDevice()? Is it because there is no instance of Phone?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Rafael Angarita wrote:But why the Phone constructor calls the overridden version of showDevice()? Is it because there is no instance of Phone?

Actually the code would be like this....

I guess now you got it..
 
 
subject: When the class will be loaded???