From See Java Static Variable Methods:

  • 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.

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 constant variables...

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.

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

    static 
    {
        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.

Answer from Lion on Stack Overflow
Top answer
1 of 7
92

From See Java Static Variable Methods:

  • 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.

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 constant variables...

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.

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

    static 
    {
        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.

2 of 7
17

Static fields are initialized when the class is loaded by the class loader. Default values are assigned at this time. This is done in the order than they appear in the source code.

🌐
Baeldung
baeldung.com › home › java › core java › when are static variables initialized in java?
When Are Static Variables Initialized in Java? | Baeldung
November 21, 2025 - In our example above, the class variable i is first initialized with an int default value of zero. The textual order applies to static fields. First, i will initialize and then j will be initialized.
🌐
Dummies
dummies.com › article › technology › programming-web-design › java › how-to-use-static-initializers-in-java-153262
How to Use Static Initializers in Java | dummies
July 2, 2025 - In Java, you can use initializer blocks to initialize instance variables. Initializer blocks aren’t executed until an instance of a class is created, so you can’t count on them to initialize static fields.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › initial.html
Initializing Fields (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
If initialization requires some logic (for example, error handling or a for loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.
🌐
TutorialsPoint
tutorialspoint.com › do-static-variables-get-initialized-in-a-default-constructor-in-java
Do static variables get initialized in a default constructor in java?
November 19, 2020 - If you declare a static variable in a class, if you haven’t initialized them, just like with instance variables compiler initializes these with default values in the default constructor. public class Sample{ static int num; static String str; static float fl; static boolean bool; public static ...
🌐
BeginnersBook
beginnersbook.com › 2013 › 05 › static-variable
Java – static variable with example
Static variables are initialized before any static method of the class executes.
🌐
Coderanch
coderanch.com › t › 383379 › java › Static-variable-initialization
Static variable initialization (Java in General forum at Coderanch)
programming forums Java Mobile ... our volunteer staff, including ... ... Hi My understanding is that all static variables and static blocks shall be initialized/executed before anyone accesses the class (static method or object creation)....
🌐
Educative
educative.io › answers › how-to-perform-static-and-instance-initialization-in-java
How to perform static and instance initialization in Java
Typically, static initialization is used to initialize static variables of a class. The block is called at the time of class initialization.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › can-we-initialize-static-variables-in-a-default-constructor-in-java
Can we initialize static variables in a default constructor in Java?
October 15, 2019 - Yes, you can also initialize these values using the constructor. ... public class DefaultExample { static String name; static int age; DefaultExample() { name = "Krishna"; age = 25; } public static void main(String args[]) { new DefaultExample(); System.out.println(DefaultExample.name); ...
🌐
javaspring
javaspring.net › blog › order-of-static-variable-initialization-java
Java Static Variable Initialization Order: Why Does This Code Output 1 Instead of 2? Explained with Examples — javaspring.net
Unlike instance variables (which ... the moment the class is loaded into the JVM until the JVM exits. ... Initialized once, when the class is first loaded....
🌐
Quora
quora.com › Would-we-ever-use-a-constructor-to-initialize-static-variables-in-Java
Would we ever use a constructor to initialize static variables in Java? - Quora
So all the objects of the class can have different set of member variables depending on what is initialised in constructor but all the objects will have the same value for static variable due to it’s independence from objects. ... Level up your Java code with IntelliJ IDEA.
🌐
Scaler
scaler.com › home › topics › java › static variable in java
Static Variable in Java with Examples - Scaler Topics
April 14, 2024 - The static keyword indicates to the JVM (Java Virtual Machine) that this variable should be loaded with the class during the compilation of the program. If we don’t initialize the static variable, the default value of the static variable data ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › static-variables-in-java
Static Variables in Java - GeeksforGeeks
August 1, 2025 - Initialization Order: The static variables are initialized before the static blocks and when the static method or block refers to a static variable, then it will use its initialized value.
🌐
Quora
quora.com › Why-cant-we-initialize-a-static-variable-inside-a-method
Why can't we initialize a static variable inside a method? - Quora
Answer (1 of 2): You added the ... which belongs to the class and not to object(instance) * Static variables are initialized only once , at the start of the execution ....
🌐
Medium
medium.com › @prithukathet › understanding-static-and-instance-initializer-blocks-in-java-0a34902ef308
Understanding Static and Instance Initializer Blocks in Java | by Prithukathet | Medium
March 7, 2025 - ... An instance initializer block is executed every time an object is created, before the constructor. It is useful when you need to initialize instance variables across multiple constructors or perform complex setup tasks.
🌐
Medium
medium.com › @iamthatsoftwareguy › java-variables-and-initialization-blocks-a-complete-guide-80aa831c32ca
Java Variables and Initialization Blocks: A Complete Guide | by iamthatsoftwareguy | Medium
October 30, 2024 - Using a Reference Variable (Even if Null): Java allows static variables to be accessed using an object reference, even if it’s null: Hello h = null; h.b = 90; Through Object Reference: Although possible, using an object reference like h.b = 90; is less common. The compiler rewrites this to Hello.b = 90;. Java classes can only contain specific types of members, and general statements can’t be placed outside method blocks. Here’s how initialization blocks are structured:
🌐
CodingTechRoom
codingtechroom.com › question › initialize-static-variables-java
How to Initialize Static Variables in Java: A Comprehensive Guide - CodingTechRoom
Declare and initialize the static variable at the point of declaration for clarity. Use static initialization blocks for complex initializations. For example: ```java public class Example { static int staticVar; static { staticVar = 42; // Initialization in a static block } } ```