• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cyclic class

 
Weerawit Maneepongsawat
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i found this code in some site.
pls explain what happen
 
Lawrence Chettiar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic