• 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

Cyclic class

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i found this code in some site.
pls explain what happen
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the answer is in the behaviour of Classloader/Intilization.
JLS: A class/interface is initialized when either its static variable/method is called for the first time, in the above case when the C is used in the main method the initilizer tries to initilize the C it reaches line int C= which uses interfaceA so it will try to initilize A when it reaches A= it uses B= so it initilize B it reaches B= which uses C again as C is in mid of initilization if a variable is used in mid of initilization the default value is returned its similar to using a variable of a subclass in super class constructor. Hence the the value returned for int B= 0 + 1 and rest is calcuated, but when the control comes to the main method the interfaceC is fully intilized it can now get value of C.
Hope this answered your question.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic