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.
-
You don't initialize them in a constructor but rather on declaration or in a static initializer block.Hovercraft Full Of Eels– Hovercraft Full Of Eels2012-11-24 20:05:10 +00:00Commented Nov 24, 2012 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– Val2012-11-24 20:27:26 +00:00Commented Nov 24, 2012 at 20:27
3 Answers
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.
2 Comments
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];
}
}
}
Comments
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.
1 Comment
initialized to loaded.