Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Final fields initialization order

Tags:

java

jls

Here is some code that calls static method A.f() on class that is not initialized yet. Can someone explain behavior of this code in terms of JLS?

class A {     final static Object b = new B();     final static int S1 = 1;     final static Integer S2 = 2;      static void f() {         System.out.println(S1);         System.out.println(S2);     } }  class B {     static {         A.f();     } }  public class App {     public static void main( String[] args )     {         A.f();     } } 

Output:

1 null 1 2 
like image 270
Sergey Alaev Avatar asked Jan 26 '15 10:01

Sergey Alaev


People also ask

In which order are instance variables initialized?

In Java, the order for initialization statements is as follows: static variables and static initializers in order. instance variables and instance initializers in order. constructors.

Can final variables be initialized?

A final variable can only be initialized once, either via an initializer or an assignment statement. There are three ways to initialize a final variable: You can initialize a final variable when it is declared. This approach is the most common.

When can final instance variables be initialized?

You can initialize final instance variables before constructor completes. A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.

Do fields need to be initialized before use?

Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.


2 Answers

A.f() in App.main() triggers initialization of class A.

All constant variables are initialized. The only constant variable is S1, which now is 1.

Then, the other static fields are initialized in textual order. b is the first field, which triggers initialization of class B, which in turn calls A.f(). S2 is simply null because it is not initialized yet. Initialization of b is now complete. Last but not least, S2 is initialized to the Integer object 2.

S2 is not a constant variable because it is not of the primitive type int but of the reference type Integer. S2 = 2; is an auto-boxing shorthand for S2 = Integer.valueOf(2);.

If a declarator in a field declaration has a variable initializer, then the declarator has the semantics of an assignment (§15.26) to the declared variable.

[…]

Note that static fields that are constant variables (§4.12.4) are initialized before other static fields (§12.4.2). This also applies in interfaces (§9.3.1). Such fields will never be observed to have their default initial values (§4.12.5), even by devious programs.

8.3.2. Field Initialization

A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28). Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9), and definite assignment (§16 (Definite Assignment)).

4.12.4. final Variables

A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

  • Literals of primitive type and literals of type String

[…]

15.28. Constant Expressions

For each class or interface C, there is a unique initialization lock LC. The mapping from C to LC is left to the discretion of the Java Virtual Machine implementation. The procedure for initializing C is then as follows:

[…]

  1. Otherwise, record the fact that initialization of the Class object for C is in progress by the current thread, and release LC.

    Then, initialize the static fields of C which are constant variables (§4.12.4, §8.3.2, §9.3.1).

[…]

  1. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

12.4.2. Detailed Initialization Procedure

Every variable in a program must have a value before its value is used:

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):

    […]

    • For all reference types (§4.3), the default value is null.

4.12.5. Initial Values of Variables

like image 75
xehpuk Avatar answered Sep 19 '22 12:09

xehpuk


It seems that this issue doesn't belong to JLS, but we have deal with JVMS.

On Linking stage before resolution process there is Preparation substage, which involves:

creating the static fields for a class or interface and initializing such fields to their default values

and more:

explicit initializers for static fields are executed as part of initialization (§5.5), not preparation

while initialization includes:

The execution of any one of the Java Virtual Machine instructions new

Initializing of primitive types includes writing their initial value. For reference type fields their default value is null because before Resolution substage jvm doesn't "know" which class is associated to apropriate symbolic reference name of a class.

like image 30
Andremoniy Avatar answered Sep 20 '22 12:09

Andremoniy