Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java static block refer to static variable in another class

public class A {
  public static String HOST;

  static {
    HOST = ...;
  }
}

public class B {
    public static String URL;

    static{
         URL = A.HOST + ...;
    }
}

My question is if A.HOST would be correctly initialised before B use it? Is this behaviour is defined in spec?

like image 334
bydsky Avatar asked Jul 15 '15 10:07

bydsky


People also ask

How do you call a static variable from another class in Java?

Static variables can be accessed by calling with the class name ClassName. VariableName. When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.

Can we call static variable in another class?

1. A static variable can be accessed inside any other class using the class name.

Can we access static and static variables from static blocks?

Static members (variables and methods) can be accessed inside static methods and static blocks only. Non-static members cannot be accessed inside static methods, blocks and inner classes.

Can a static method reference static variable in Java?

From inside static methods you can only access static variables or call static methods of the class. Show activity on this post. The static members can be used without an instance of the class, but your c variable will only start to exist once the class is instantiated.


Video Answer


1 Answers

Yes that behavior is well defined here.

In short, citing from that link

Initialization of a class or interface consists of executing the class or interface initialization method <clinit>

...

A class or interface may be initialized only as a result of:

The execution of any one of the Java Virtual Machine instructions new, getstatic, putstatic, or invokestatic that references the class or interface (§new, §getstatic, §putstatic, §invokestatic). All of these instructions reference a class directly or indirectly through either a field reference or a method reference.

Upon execution of a new instruction, the referenced class or interface is initialized if it has not been initialized already.

Upon execution of a getstatic, putstatic, or invokestatic instruction, the class or interface that declared the resolved field or method is initialized if it has not been initialized already.

The first invocation of a java.lang.invoke.MethodHandle instance which was the result of resolution of a method handle by the Java Virtual Machine (§5.4.3.5) and which has a kind of 2 (REF_getStatic), 4 (REF_putStatic), or 6 (REF_invokeStatic).

Invocation of certain reflective methods in the class library (§2.12), for example, in class Class or in package java.lang.reflect.

The initialization of one of its subclasses.

Its designation as the initial class at Java Virtual Machine start-up (§5.2).

The <clinit> method is the method (created by the compiler) that initializes static variables and has the code that you put in the static block

In your case, when the static block of class B runs (which is what <clinit> will do), it will have a getStatic opcode, requesting A.HOST. So the initialization of A will be triggered, and A.HOST initialized. So you will read the proper value.

like image 150
xp500 Avatar answered Oct 14 '22 12:10

xp500