36

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

I would really appreciate the help.

Thank you.

suprasad
  • 1,411
  • 3
  • 18
  • 40

8 Answers8

86

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.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • 1
    It's worth mentioning that the instance initialization blocks will be executed before any constructor code and after calling the parent constructor. – Ahmed Hegazy Mar 04 '16 at 14:15
  • @AdriaanKoster is there any way to print class loading logs for debugging purpose? – Gaurav May 09 '19 at 15:34
  • 1
    @gaurav Yes, start the JVM with -verbose:class See also this question: https://stackoverflow.com/questions/10230279/java-verbose-class-loading – Adriaan Koster May 10 '19 at 09:01
22

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.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • So moment you start the executable, if you have multiple classes, all their static variables will be loaded on the heap? – committedandroider Dec 21 '14 at 19:57
  • 2
    @committedandroider: it is a bit more complicated - the static variables get initialized just before the class is used anywhere – Ralph Dec 22 '14 at 10:24
6

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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 2
    by runtime, do you mean when the class is loaded or when that field is first referenced? – Bhushan Dec 08 '11 at 21:05
  • @Jigar is there any way to print class loading logs for debugging purpose? – Gaurav May 09 '19 at 15:32
  • 1
    @gaurav try `-verbose:class` VM option. https://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbmtm – jmj May 09 '19 at 18:56
4

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.

user2206366
  • 461
  • 3
  • 6
  • 17
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

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

musiKk
  • 14,751
  • 4
  • 55
  • 82
2

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

user207421
  • 305,947
  • 44
  • 307
  • 483
1

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.

Marko
  • 20,385
  • 13
  • 48
  • 64
1

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

Deepak Silver
  • 27
  • 1
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31696979) – Dan May 09 '22 at 19:29