4

Below is the code,

class Z{
    static int peekj(){
        return j;
    }
    static int peekk(){
        return k;
    }
    static int i = peekj();
    static int h = peekk();
    static final int j = 1;
    static int k = 1;
}


public class ClassAndInterfaceInitialization {
    public static void main(String[] args) {
        System.out.println(Z.i);
        System.out.println(Z.h);
    }
}

After following the forward reference rule for static initialization, I see the output as:

1
0

After class Z is loaded & linked, In the initialization phase, variable j being final is very firstly initialized with 1. variable k is also initialized with 1.

But the output gives 0 for variable k.

How do I understand this?

Note: Compiler actually replaces value of variable j wherever it is referenced following forward reference rules, unlike k

2
  • One more question : System.out.println(Z.peekk()); outputs 1 where as System.out.println(Z.h); outputs 0. How? Commented Aug 18, 2015 at 5:18
  • @overexchange Yes All the 'J' occurences are replaced by actual Number 1 . See my answer below. Commented Aug 18, 2015 at 5:53

3 Answers 3

6

static final int will make j a compile-time constant. So, its value will be present and passed as part of the byte-code itself. So, j will be 1 when your class is being initialized.But k is is just static int, so its default value will be printed since your static initializer runs before the value of k is initialized.

Sign up to request clarification or add additional context in comments.

5 Comments

One more question : System.out.println(Z.peekk()); outputs 1 where as System.out.println(Z.h); outputs 0. How?
Once System.out.println(Z.peekk()) is called, all of the variables are properly initialized (to 1).
In main method : System.out.println(Z.i); System.out.println(Z.peekk()); System.out.println(Z.h);. Output: 1 1 0
In main method: System.out.println(Z.peekk()); System.out.println(Z.h);. Output: 1 0. No change in output. Note: Even I change the order of SOP then also the behavior is same.
@NamanGala - Thats because when h was being initialized, k was 0. But peekk() returns the current value of k when the method is called. You are calling method after the class is initialized. SO, value is 1. got it?
5

You are trying to access k before it is initialized, which is why it is 0. (h is initialized to peekk() before k is defined, so peekk() returns 0, as fields are initialized from the top down.) j is final, so it is execute first; before the declaration of i, thus i gets the value of j or 1.

Here's the appropriate Oracle documentation:

Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

5 Comments

The same is happening for j also. But it's giving proper result because of compile-time replacement (as stated in TheLostMind's answer).
@TimBiegeleisen I did it.
One more question : System.out.println(Z.peekk()); outputs 1 where as System.out.println(Z.h); outputs 0. How?
When you say, "trying to access k before it is initialized, which is why it is 0", Are you saying that, there is a separate phase prior to initialization phase, where k is set to 0? Is yes, what is that phase?
Basically. Java will provide default values to everything before everything else, before all other code is run (at compile time, IIRC).
3

"j" is a constant so when the code is compiled, j is replaced by the number 1 in the whole program. So if you were an alien and you could read byte code the code would look like this

 static int peekj(){
        return 1;// j is replaced by the Actual Number 1
    }

Also "static variables are initialized when the class is loaded" So when the class loads the following statements are executed in sequential manner

     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 i.e 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's 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 output 1 and 1 i.e i = 1 and h =1; as the value of k is being set to 1 first.

  static int k = 1;
 static int i = peekj();
    static int h = peekk();
    static final int j = 1;

2 Comments

One supple,What are the defaults values of different primitive data types?
@overexchange Click Here docs.oracle.com/javase/tutorial/java/nutsandbolts/… And Scroll down to see the table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.