File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When the class will be loaded???

 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rafael Angarita
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic