*
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
Author

compile time constants

sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
package
sample;
public
class StaticVar {
/**
* @param args
*/
public static void main(String[ ] args) {
// TODO Auto-generated method stub
System.
out.println(x);
}
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 :



Saravanan
SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
 
jQuery in Action, 2nd edition
 
subject: compile time constants
 
Similar Threads
illegal Forward Reference...
initialization of static variables
simple prog.
abstract class instantiation
forward referencing