4

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.

4
  • 1
    statics loads before the class is loaded. That's why it is first 0, because the method is not ready to use. Commented Apr 23, 2015 at 11:33
  • statics loads before the class, so when test() return 0 when it is first executed. thanks Commented Apr 23, 2015 at 11:44
  • A addition question.if I add static{ System.out.print(first); } before first is declared ,there comes with a error.Why? if statics is initialized before class is loaded , the code above should be valid. Commented Apr 23, 2015 at 12:00
  • @hsc You still cannot directly use a variable before it is initialised but you can use it indirectly in the same manner you did for first ideone.com/hGqlX5 Commented Apr 23, 2015 at 12:12

4 Answers 4

4

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.

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

6 Comments

static int first=test(); line will call test() but why did it return second default value rather that giving a compiler error saying second not declared
@singhakash the compiler doesn't trace through the code of the method call to determine if you use a variable which hasn't been initialised. This is an unsolvable halting problem in the general case. The only way to know this is to actually run time code. There is a deliberate limit on how far the compiler will go to perform this check.
No I am saying,as you said java is top to bottom so first line is static int first=test(); which calles test() but havnt declared second variable yet then why test() can return a variable which is not declared.I am talking about declaration not initialization
@singhakashfields are not declared dynamically. Every field already exist in the byte code and the object which holds all the fields (static or non-static) has exactly enough space to hold all the fields the class will ever have. There is no point at which you can see the class or an instance where the fields are not completely allocated. The only thing you can see when when they are being set.
@singhakash even for local variables, space is reserved on the stack as soon as the method call starts, it doesn't allocate memory on the stack on a line by line basis.
|
0

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;
}

Comments

0

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.

Comments

0

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.

Comments

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.