• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When the class will be loaded???

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
reply
    Bookmark Topic Watch Topic
  • New Topic