aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain why the compiler is not complaining and why the output is 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain why the compiler is not complaining and why the output is 0" Watch "please explain why the compiler is not complaining and why the output is 0" New topic
Author

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

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Aurelian Tutuianu
Ranch Hand

Joined: May 13, 2004
Posts: 86
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

http://javasign.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain why the compiler is not complaining and why the output is 0