5

In a Java class with static instance fields, is the constructor called every time the fields are accessed, or only on the first access? I initialize the static fields in the constructor, and was wondering if this would cause a slow down because the fields are initialized on every access.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
dubreakkk
  • 199
  • 2
  • 10
  • You don't initialize them in a constructor but rather on declaration or in a static initializer block. – Hovercraft Full Of Eels Nov 24 '12 at 20:05
  • In java, static and instance are mutually exclusive states. You cannot have a "static instance" field. Statics can be initialized by static initializers. Constructors exist to initialize instance fields, they are "instance initializers". – Val Nov 24 '12 at 20:27

3 Answers3

14

I initialize the static fields in the constructor,

Don't. Never initialize static fields inside a constructor. static fields are not something associated with any instance of a class. It is bound to class. There is only single copy of that variable, that is accessed accross all the instances. So, if you are initializing it in constructor, then every time you create an instance, that field will be re-initialized for every other instance.

You should use static initializer block to initialize your static fields, or just initialize them at the place of declaration.

class Demo {
    private static int x;  // Either initialize it here.

    static {   // Or use static initializer block
        x = 10;
    }
}

with static instance fields, is the constructor called every time the fields are accessed,

No. , static fields are accessed on class. They are loaded and initialized when the class is loaded. And then you can modify it later on, on class name, in which case, the change will be effected for all the instances. So, the constructor will not be invoked, whenever you access static field.

In fact, even when you access instance field, constructor is not invoked every time. Constructor is used to initialize the state of the newly created instance once. And for further access and modification of that field, constructor won't be invoked.

So, a constructor has precisely no role to play whenever you want to access any fields of your class.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • There is one (that I can think of) time when initialising statics in a constructor is useful and that is when you are making a singleton. – OldCurmudgeon Nov 24 '12 at 20:19
  • @OldCurmudgeon.. Umm. Yeah, because in that case, you would have only a single instance of your class moving around. So, there will be no inconsistent state of static field moving around the application. – Rohit Jain Nov 24 '12 at 20:24
2

The constructor for the object of the static field is called only once, at some point before the field is accessed for the first time. You should not initialize static fields in a regular instance constructor. If they need special initialization, you should supply a static initialization block, like this:

public class Test {
    static int[][] a = new int[20][];
    static {
        for (int i = 0 ; i != 20 ; i++) {
            a[i] = new int[i+1];
        }
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

static variables are loaded when the class is loaded. And only once. According to the JLS:

If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized

So this answers your question. i.e.e exactly when the class is loaded :)

Static variables lasts till the JVM is shutdown.

Jatin
  • 31,116
  • 15
  • 98
  • 163
  • 1
    Your first statement is somewhat incorrect. static variables are loaded only once, but they are not initialized only once. They can be re-initialized any time. I think you should re-frame that statement. Change the `initialized` to `loaded`. – Rohit Jain Nov 24 '12 at 20:28