1

I am studying static initializers in Java. I came through a source code as given below:

    public class A {
       private static int count = 5;
       final static int STEP = 10;
       boolean alive;
       static {
         count = 1;
       }
       public static void main(String[] args) {
          A a = new A();
          System.out.println(A.count);
       }
    }

My question is that why the compiler is not complaining about the variable count being reassigned the value 1 in count = 1 in the static initializer block. I know that Java allows forward referencing as long as declaration-before-read rule (i.e. any identifier should not be read before declaration) is followed which is for all initializers and if not then the reference (or identifier) must come on the left-hand side of the assignment. I also know that if multiple static initializer expressions and static field initializer blocks are written in a class then their order of execution is sequential.

According to me the flow of execution should be: The class is loaded and then all the static initializer (expressions and blocks) are executed in sequence, due to which the count will be initialized to value 5 and then the default constructor will be executed calling the super() and initializing the instance variable alive to default value. But why it is not throwing an error that static variable count has been re-initialized (as it is not the case of forward reference), instead, it gives the output 1. Does that mean that we can re-initialize static variables through static initializer blocks?

Cœur
  • 37,241
  • 25
  • 195
  • 267
patrick.1729
  • 4,222
  • 2
  • 20
  • 29

2 Answers2

2

Since your class variable is appearing before use, that is ok.

Here is the language specification#Static Initializers for the same

A static initializer declared in a class is executed when the class is initialized (§12.4.2). Together with any field initializers for class variables (§8.3.2), static initializers may be used to initialize the class variables of the class.

Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See §8.3.3 for the precise rules governing forward reference to class variables.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • complementing: in this question, the initializer block could appear before the declaration since the field is only used in the left hand side of an assignment. – user85421 Jul 04 '17 at 11:40
  • Thank you both but the answer given by @al3xkr is more clear and conceptual. – patrick.1729 Jul 04 '17 at 15:16
1

As long as you manipulate the static variable within the class it is initialized you won't get an error from the compiler. If you had declared it as final the behavior you mentioned would have occurred. Keep in mind that static means that the variable is shared across all instances of the object, so it only gets initialized and memory allocated once.

user207421
  • 305,947
  • 44
  • 307
  • 483
al3xkr
  • 46
  • 8
  • Thanks! It helped to clear the concept. I tried reassigning the variable in constructor and instance initializer and it worked! :) – patrick.1729 Jul 04 '17 at 15:13