File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile time constants 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 "compile time constants " Watch "compile time constants " New topic

compile time constants

sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
class StaticVar {
* @param args
public static void main(String[ ] args) {
// TODO Auto-generated method stub
private static int x=getValue();
private static int y=5;
public static int getValue(){
return y;

how the output of program is 0 instead of 5 i think it may be of runtime constants can anyone explain the why the output is like that and what is runtime constants
Thirugnanam Saravanan
Ranch Hand

Joined: Dec 13, 2007
Posts: 81
private static int x=getValue();

During this assignment, the value of static instance variable Y is 0 and is not initialized to 5. In the next line only you are initializing Y to 5. That's why the output is 0.

If the output should have been 5, then you have to initialize the Y value before you actually use that value. Refer the below code to get the Output of 5 :

SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
I agree. Here's the link:
subject: compile time constants
It's not a secret anymore!