When does a static variable load in java, runtime or compile time?

When does a static variable load, run time or compile time? Can someone explain this.

I am very grateful for the help.

Thanks.

+24
source share
7 answers

They are loaded at runtime.

Staticity means that the variable belongs to the class, not to instances of the class. Thus, there is only one value of each static variable, and not n values, if you have n instances of the class.

+12
source

The compiler optimizes the built-in static final fields by inserting the value into the bytecode instead of calculating the value at run time.

When you start the JVM and load the class for the first time (this is done by the class loader the first time the class is accessed), any static blocks or fields are loaded into the JVM and become available.

Demonstration:

public class StaticDemo { // a static initialization block, executed once when the class is loaded static { System.out.println("Class StaticDemo loading..."); } // a constant static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000; // a static field static int instanceCounter; // a second static initialization block // static members are processed in the order they appear in the class static { // we can now acces the static fields initialized above System.out.println("ONE_DAY_IN_MILLIS=" + ONE_DAY_IN_MILLIS + " instanceCounter=" + instanceCounter); } // an instance initialization block // instance blocks are executed each time a class instance is created, // after the parent constructor, but before any own constructors (as remarked by Ahmed Hegazy) { StaticDemo.instanceCounter++; System.out.println("instanceCounter=" + instanceCounter); } public static void main(String[] args) { System.out.println("Starting StaticDemo"); new StaticDemo(); new StaticDemo(); new StaticDemo(); } static { System.out.println("Class StaticDemo loaded"); } } 

Output:

 Class StaticDemo loading... ONE_DAY_IN_MILLIS=86400000 instanceCounter=0 Class StaticDemo loaded Starting StaticDemo instanceCounter=1 instanceCounter=2 instanceCounter=3 

Note that "Starting StaticDemo" does not appear as the first line of output. This is because the class must be loaded before , the main method can be executed, which means that all static fields and blocks are processed in order.

+74
source

runtime when loading a class. - Look at the initialization

+4
source

Static fields are loaded when the class is loaded. This usually happens when a class object file is created, but it may be earlier if the class is used in another way.

The static initializer is thread safe and you can safely access the class in multiple threads. This is useful as a way to create a stream safe, without having to use a lock.

Note. A class can be loaded (and its static initialization loop) more than once if multiple class loaders are used. As a rule, loading the same class in several class loaders can be confusing and can be avoided, but it is supported and works.

+2
source

How would you load a variable at compile time? The variable is initialized when the corresponding class is loaded. See JVMS .

+1
source

Download is performed at runtime. Everything loads at runtime.

0
source

When you enter java ClassName , then the class is loaded into the JVM with static variables, so you don't need an object for it.

Where as the instance variable loaded by the JVM when creating the object.

0
source

Source: https://habr.com/ru/post/989931/


All Articles