• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain why the compiler is not complaining and why the output is 0

 
Jacob Sonia
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles because main can acces prvate variable i because main is in the class which holds i. So it can access everything from that class.

It gives 0 because the initialization of member variable i is done BEFORE the initialization of the member variable j. So first giveMeJ return default value of j, which is 0 for int type.

See JLS - 8.3.2.3 Restrictions on the use of Fields during Initialization
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic