hsc
hsc

Reputation: 1298

How java initialize static variables and static method?(with simple code)

I wonder how java initialize these static variables. The code I can't understand is shown blow:

public class Main {

static int first=test();
static int second=2;
static int third=test();

public static void main(String[] args) {

    System.out.println(first);
    System.out.println(second);
    System.out.println(third);

}


public static int test() {
    return second;
}

}

The output of the simple code below is 0 2 2

If the compiler will ignore non-executable method automatically or the static variable is 0 before it is define?

Sorry for can't find accurate description to google it.

Upvotes: 4

Views: 2109

Answers (4)

Raúl
Raúl

Reputation: 1552

Let's try to understand things from JVM perspective:

Loading - JVM find the binary representation of a Type (class/interface) with a particular name and creates class/interface from that binary representation. Static members (variables / initialization block) are loaded during class loading - in order of their appearance in code.

Here, as part of loading - static members are getting executed in order of occurrence. Method test() is being called twice; but first time, variable second was just declared, not defined (initialized) - hence defaulted to 0. After that, it gets value 2 and prints - 0 2 2.

Try placing a static block in the code & put some debug points - you will see it yourself.

Upvotes: 0

xuma202
xuma202

Reputation: 1084

You could test if the compiler ignores the non-executable method by putting some print statements in it

public static int test() {
    System.out.println("Test exceuted")
    return second;
}

now your output will probably be:

Test exceuted
Test exceuted
0
2
2

this shows us that the method is executed but because second is not yet initialized it returns 0 the first time.

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

This may help you.

// executing order top to bottom
static int first=test();//test() will return value of second still second is 0
static int second=2;//now second will be 2
static int third=test();//test() will return current value of second, which is 2

public static void main(String[] args) {

    System.out.println(first);// so this will be 0
    System.out.println(second); // this will be 2
    System.out.println(third); // this will be 2

}

public static int test() {
    return second;
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533492

When Java executes code it is from top to bottom. So when it initialises variables it does so from top to bottom. However, when you read an unitilised value it will be 0, null or false as the memory is first filled with zeros. Note: the static fields of a class have their own special object which you can see in a heap dump.

so when you try to set

first = 0; // second hasn't been set yet
second = 2;
third = 2; // second has been set to 2.

The addition of the method only prevents the compiler from detecting you are trying to use a variable before it was initialised.

Upvotes: 4

Related Questions