Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is static variable loaded in java, runtime or compile time?

Tags:

java

When is static variable loaded, runtime or compile time? Can someone please explain it.

I would really appreciate the help.

Thank you.

like image 385
suprasad Avatar asked Dec 03 '10 08:12

suprasad


People also ask

Are static variables compile time?

After static initialization, dynamic initialization takes place. Dynamic initialization happens at runtime for variables that can't be evaluated at compile time 2. Here, static variables are initialized every time the executable is run and not just once during compilation.

When static variables are initialized in Java?

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.

When should we declare a variable as static in Java?

Static variables are created when the program starts and destroyed when the program stops. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. Default values are same as instance variables.

Can static variables be changed during runtime?

Static variables belong to the class instead of to the individual instances, so it doesn't change in every instance - it only exists in one place.


8 Answers

The compiler optimizes inlineable static final fields by embedding the value in the bytecode instead of computing the value at runtime.

When you fire up a JVM and load a class for the first time (this is done by the classloader when the class is first referenced in any way) any static blocks or fields are 'loaded' into the JVM and become accessible.

A 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 

Notice how '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 all static fields and blocks are processed in order.

like image 167
Adriaan Koster Avatar answered Sep 29 '22 09:09

Adriaan Koster


They are loaded at runtime.

Static means: that the variable belong to the class, and not to instances of the class. So there is only one value of each static variable, and not n values if you have n instances of the class.

like image 30
Ralph Avatar answered Sep 29 '22 09:09

Ralph


run time when class is loaded. - Have a look at initialization

like image 21
jmj Avatar answered Sep 29 '22 09:09

jmj


The static fields are loaded when the class is loaded. This usually happens which the file object of a class is created, but it can be earlier if the class is used another way.

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

Note: the class can be loaded (and its static intialisation block run) more than once if multiple class loaders are used. Generally, loading the same class in multiple class loaders can be confusing and is avoided, but it is supported and does work.

like image 22
Peter Lawrey Avatar answered Sep 29 '22 08:09

Peter Lawrey


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

like image 31
musiKk Avatar answered Sep 29 '22 10:09

musiKk


Loading is a runtime operation. Everything is loaded at runtime.

like image 22
user207421 Avatar answered Sep 29 '22 09:09

user207421


When you type java ClassName then class loads into JVM with static variables, so you don't need an object for it.

Where as instance variable loaded by JVM when the object is created.

like image 34
Ben Tennyson Avatar answered Sep 29 '22 10:09

Ben Tennyson


Static fields gets loaded at the time of class loading in JVM.

like image 41
Deepak Silver Avatar answered Sep 29 '22 09:09

Deepak Silver