Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java static initialization behaviour

Tags:

java

static

public class Hello {
        public static final Hello h = new Hello();
        static int i = 5;
        int j  = i;

        private void print() {
            System.out.println(i+" , "+j);

        }
        public static void main(String[] args) {

            h.print();
        }

    }

This code output is 5 , 0. if reason is static loads first in class and i is initialized and j is not.but if i remove static from i also

public class Hello {
        public static final Hello h = new Hello();
        int i = 5;
        int j  = i;

        private void print() {
            System.out.println(i+" , "+j);

        }
        public static void main(String[] args) {

            h.print();
        }

    }

now why output is 5,5. then when i and j is initialized. please explain the reason.

like image 961
ankita gahoi Avatar asked Jun 21 '13 11:06

ankita gahoi


People also ask

What is static initialization in Java?

A Static Initialization Block in Java is a block that runs before the main( ) method in Java. Java does not care if this block is written after the main( ) method or before the main( ) method, it will be executed before the main method( ) regardless.

How do you initialize a static method in Java?

Instance variables are initialized using initialization blocks. However, the static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded.

Can static methods be initialized?

A static initializer declared in a class is executed when the class is initialized (§12.4. 2). Together with any field initializers for class variables (§8.3. 2), static initializers may be used to initialize the class variables of the class.

Is static initialization thread safe Java?

Yes, Java static initializers are thread safe (use your first option). However, if you want to ensure that the code is executed exactly once you need to make sure that the class is only loaded by a single class-loader. Static initialization is performed once per class-loader.


5 Answers

The static block is executed in order.

You first create a Hello object, at this point i = 0 as it hasn't been set.

Only after this does i = 5

You have to read the static statements from top to bottom.

like image 97
Peter Lawrey Avatar answered Oct 14 '22 16:10

Peter Lawrey


Non-static variables i and j are initialized that moment when Hello object is created:

public static final Hello h = new Hello();

For the first part of the question Peter gave you an answer. Let me complement it. If you changed the order of static variables:

static int i = 5;
public static final Hello h = new Hello();
int j  = i;

it would print 5, 5 and not 5, 0.

like image 43
darijan Avatar answered Oct 14 '22 15:10

darijan


Try to swap the public static final Hello h = new Hello(); and static int i = 5; lines. You initialize first the hello object (when i = 0, uninitialized) and than i. Init the i first to get an expected behavior.

like image 33
SeniorJD Avatar answered Oct 14 '22 15:10

SeniorJD


Here's what happens in your first example:

  • Static memory is initialized to 0. At this point Hello.i equals 0.
  • Hello.h is instantiated:
    • Hello.h.j is initialized to Hello.i's current value, i.e. 0.
  • Hello.i is initialized to 5.

In your second example, on the other hand:

  • Hello.h is instantiated:
    • Hello.h.i is initialized to 5.
    • Hello.h.j is initialized to Hello.h.i's current value, i.e. 5.
like image 38
Nicola Musatti Avatar answered Oct 14 '22 16:10

Nicola Musatti


Peter Lawrey's answer is correct, your confusion may be coming from the fact everything is in a single class and the names are like so i wanted to give you another way to visualize what your doing, so your code is logically equivalent to the following code:

public class Program {

    public static Hello h = new Hello();

        public static void main(String [] args) {
            h.i = 5;
            h.print();
        }
    }

    class Hello {

        public static int i = 0;
        private int j = i;

        public void print() {
            System.out.println(i+", "+j);
        }
    }
like image 44
Ibrahim Najjar Avatar answered Oct 14 '22 17:10

Ibrahim Najjar