Ankit Ankit - 2 months ago 6x
Java Question

when static variables are initialized in Java

static variables are initialized to default values.
I am wondering when that happens?
when a class is loaded, static vars are created(allocated space),
then static initializers and initializations in declarations are executed.
At what point, the default values are given?
This leads to the problem of forward reference.

I am totally confused over this topic of static field variables.
Also please if you can explain this in reference to the question asked on Why static fields are not initialized in time? and especially the answer given by Kevin Brock on the same site. I can't understand the 3rd point.

  • It is a variable which belongs to the class and not to object(instance)
  • Static variables are initialized only once , at the start of the execution .
  • These variables will be initialized first, before the initialization of any instance variables
  • A single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object. See

Instance and class (static) variables are automatically initialized to standard default values if you fail to purposely initialize them. Although local variables are not automatically initialized, you cannot compile a program that fails to either initialize a local variable or assign a value to that local variable before it is used.

What the compiler actually does is to internally produce a single class initialization routine that combines all the static variable initializers and all of the static initializer blocks of code, in the order that they appear in the class declaration. This single initialization procedure is run automatically, one time only, when the class is first loaded.

In case of inner classes, they can not have static fields

An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields"

See JLS 8.1.3 Inner Classes and Enclosing Instances

final fields in Java can be initialized separately from their declaration place this is however can not be applicable to static final fields. See the example below.

final class Demo
    private final int x;
    private static final int z;  //must be initialized here.

        z = 10;  //It can be initialized here.

    public Demo(int x)
        this.x=x;  //This is possible.
        //z=15; compiler-error - can not assign a value to a final variable z

This is because there is just one copy of the static variables associated with the type, rather than one associated with each instance of the type as with instance variables and if we try to initialize z of type static final within the constructor, it will attempt to reinitialize the static final type field z because the constructor is run on each instantiation of the class that must not occur to static final fields.