"j" is a constant, so when the code compiles, j is replaced by number 1 in the entire program. So if you were an alien and you could read the byte code, the code would look like this:
static int peekj(){ return 1;
Also, "static variables are initialized when the class is loaded" So, when the class is loaded, the following statements are executed sequentially
static int i = peekj(); // Executed First, as peekJ() directly returns 1 so i is equal to 1 static int h = peekk(); // Executed Second, Now peekk() returns "THE VALUE OF k" which is not set yet ie default value of int is given 0(zero) //So h is equal to 0 static final int j = 1; // This statement doesn't exist as all j are replaced with actual Number 1 when we compiled java to .class static int k = 1; // Executed last, So now the value of 'k' is being set, after the value of 'h' has already been set to zero
Note: Do this to get the output 1 and 1 ie i = 1 and h = 1; since the value of k is first set to 1.
static int k = 1; static int i = peekj(); static int h = peekk(); static final int j = 1;
source share