Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static initializer error if placed before the declaration

I noticed something in static initializers which may be a bug in the javac. I have constructed a scenario where I can assign a variable a value but not read that value back.

The two examples are below, the first compiles fine, the second gets an error when trying to read a value from tmp, but for some reason assigning a value to tmp is permitted. I could understand if it could neither read nor write to the variable since tmp is declared after the static initializer, but an error on only one of those does not make sense to me.

//Compiles Successfully:
public class Script
{
    public static Object tmp;
    static
    {
        tmp = new Object();
        System.out.println(tmp);
    }

}

//error only on the read but not the assignment
public class Script
{

    static
    {
        tmp = new Object();
        System.out.println(tmp);
    }
    public static Object tmp;
}

to emphasize the point further, this does compile successfully.

public class Script
{

    static
    {
        tmp = new Object();
    }
    public static Object tmp;
}
like image 965
S E Avatar asked May 07 '13 13:05

S E


People also ask

When the static initializers are executed?

These blocks are only executed once when the class is loaded. There can be multiple static initialization blocks in a class that is called in the order they appear in the program.

What are static initializers and when would you use them?

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.

Can static variables be re initialized?

Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block.

What are static blocks and static initializers in Java?

Static Block In Java, a static block executes code before the object initialization. A static block is a block of code with a static keyword: static { // definition of the static block } Static initializer block or static initialization block, or static clause are some other names for the static block.


1 Answers

It seems this is defined in the spec (See JLS 8.3.2.3):

The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

  • The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer
    of C.

  • The usage is not on the left hand side of an assignment.

  • The usage is via a simple name.

  • C is the innermost class or interface enclosing the usage.

So if the usage is on the left hand side of an assignment, then it is legal, since the second one does not hold anymore.

like image 181
zw324 Avatar answered Nov 14 '22 23:11

zw324