16

One of my friends asked me that which will load first static variable or static block.

My answer points to static variable.

So he gave me two equations and said to differentiate between them
First Equation

public class Some {
    public static void main(String args[])
    {
        System.out.println(Some.x);
    }
    static {
        System.out.println(Some.x);
    }
    static int x=90;
}

O/P: 0 90

Second Equation

public class Some {
    public static void main(String args[])
    {
        System.out.println(Some.x);
    }
    static int x=90;
    static {
        System.out.println(Some.x);
    }
}

O/P: 90 90

I tried to decompile the byte code and found it's same for both the above equation. Please help me to differentiate between them. I am confused when the static variable will initialised.

4
  • 3
    Static stuff is executed sequentially, in the order in which it is defined in the class. Mar 15, 2013 at 8:32
  • Are you sure the byte code is the same? I would have expected the declarations to appear in a different order, and as @Sudhanshu points out, the declaration order matters. Mar 15, 2013 at 8:54
  • @PatriciaShanahan ya for both the equation byte code is same.
    – Hablu
    Mar 15, 2013 at 9:00
  • @Hablu Is not same generate the byte code.
    – Ng Sharma
    Jul 9, 2019 at 10:56

4 Answers 4

8

Static blocks are initialised in the order they appear in the source file. There are several questions relating to this on stack overflow already... This one has a good answer for you: Java : in what order are static final fields initialized?

2

static variables and static blocks are executed in an order in which they appear.

Here first O/P: 0 90 as in the System.out.println(Some.x); statement of the static block executed after the static variable initialization statement static int x=90;

2
  • Rubbish. Constant folding is done by the compiler, not the JVM. The JVM executes static variable initializers and blocks in the textual order that they appear in the source file. Not variables first and then blocks.
    – user207421
    Jan 29, 2017 at 14:00
  • Edited, sorry for the inconvenience and thanks. Jan 29, 2017 at 17:30
-1

static Variables are executed when the JVM loads the Class, and the Class gets loaded when either its been instantiated or its static method is being called.

static Initializer Block gets Initialized before the Class gets instantiated or before its static method is called, and Even before its static variable is used.

-1

I am giving a simple example for control flow of static and instance stuffs:

Suppose you have 2 clases A and B. class A extends to class B. and class B has a main method. After successful compilation of both your command on cmd is like:

java B

Now what will happen see step by step:

  1. classes A and B will be loaded
  2. static members and blocks of class A will be identified and will be executed sequentially(one by one)(But only once at the time of class loading)
  3. static members and blocks of class B will be identified and executed(one by one)(But only once at the time of class loading)
  4. main method of class B will be invoked(In case class B don't have a main method then class A's main method will be invoked)
  5. As soon as you will create an object of class A : all instance members initialization and instance block execution will be done in class A
  6. the constructor of class A(which you used for creating object) will be executed
  7. If you create an object of class B: all instance members initialization and instance block execution will be done in class A.
  8. the constructor of class A(default constructor or any other if you called it from B's constructor) will be executed

  9. then all instance members initialization and instance block execution will be done in class B

  10. and after that the constructor of class B(which you used for creating object) will be executed

Note: static members and blocks execution is done only one time while loading class for first time, while instance members and instance blocks are executed each time as we create an object of the class. Please let me know if I am not correct.

3
  • 'Class A extends to class B' is ambiguous at best, or meaningless at worst. If you mean that A extends B most of your answer is wrong.
    – user207421
    Jan 29, 2017 at 14:02
  • Couldn't get you, please explain your point. Apr 28, 2017 at 6:32
  • I don't think I can. It already seems perfectly clear to me. You need to clarify the statement I quoted.
    – user207421
    Apr 28, 2017 at 7:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.