From See Java Static Variable Methods:
- It is a variable which belongs to the class and not to object(instance)
- 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.
Instance and class (static) variables are automatically initialized to standard default values if you fail to purposely initialize them. Although local variables are not automatically initialized, you cannot compile a program that fails to either initialize a local variable or assign a value to that local variable before it is used.
What the compiler actually does is to internally produce a single class initialization routine that combines all the static variable initializers and all of the static initializer blocks of code, in the order that they appear in the class declaration. This single initialization procedure is run automatically, one time only, when the class is first loaded.
In case of inner classes, they can not have static fields
An inner class is a nested class that is not explicitly or implicitly declared
static....
Inner classes may not declare static initializers (§8.7) or member interfaces...
Inner classes may not declare static members, unless they are constant variables...
See JLS 8.1.3 Inner Classes and Enclosing Instances
final fields in Java can be initialized separately from their declaration place this is however can not be applicable to static final fields. See the example below.
Copyfinal class Demo
{
private final int x;
private static final int z; //must be initialized here.
static
{
z = 10; //It can be initialized here.
}
public Demo(int x)
{
this.x=x; //This is possible.
//z=15; compiler-error - can not assign a value to a final variable z
}
}
This is because there is just one copy of the static variables associated with the type, rather than one associated with each instance of the type as with instance variables and if we try to initialize z of type static final within the constructor, it will attempt to reinitialize the static final type field z because the constructor is run on each instantiation of the class that must not occur to static final fields.
From See Java Static Variable Methods:
- It is a variable which belongs to the class and not to object(instance)
- 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.
Instance and class (static) variables are automatically initialized to standard default values if you fail to purposely initialize them. Although local variables are not automatically initialized, you cannot compile a program that fails to either initialize a local variable or assign a value to that local variable before it is used.
What the compiler actually does is to internally produce a single class initialization routine that combines all the static variable initializers and all of the static initializer blocks of code, in the order that they appear in the class declaration. This single initialization procedure is run automatically, one time only, when the class is first loaded.
In case of inner classes, they can not have static fields
An inner class is a nested class that is not explicitly or implicitly declared
static....
Inner classes may not declare static initializers (§8.7) or member interfaces...
Inner classes may not declare static members, unless they are constant variables...
See JLS 8.1.3 Inner Classes and Enclosing Instances
final fields in Java can be initialized separately from their declaration place this is however can not be applicable to static final fields. See the example below.
Copyfinal class Demo
{
private final int x;
private static final int z; //must be initialized here.
static
{
z = 10; //It can be initialized here.
}
public Demo(int x)
{
this.x=x; //This is possible.
//z=15; compiler-error - can not assign a value to a final variable z
}
}
This is because there is just one copy of the static variables associated with the type, rather than one associated with each instance of the type as with instance variables and if we try to initialize z of type static final within the constructor, it will attempt to reinitialize the static final type field z because the constructor is run on each instantiation of the class that must not occur to static final fields.
Static fields are initialized when the class is loaded by the class loader. Default values are assigned at this time. This is done in the order than they appear in the source code.
Videos
It gets value which you provide i.e, 10. Static variables are loaded at runtime
When you fire up a JVM and load the class StaticKindOfThing, then static blocks or fields(Here a, b) are 'loaded' into the JVM and become accessible.
From here:-
- It is a variable which belongs to the class and not to object(instance)
- 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
EDIT:-
Please go through the Detailed Initialization Procedure
No, it gets value you provided, aka 10.
In your case even you write:
static int a;
the result will be 0. Because you didn't give any value.
Sometimes you can write static block like:
static {
//...
}
to be sure that this block runs first before class initiation.
The static initializer block is executed only once when the class is loaded into the JVM like static variable.
Try this one that will do what you think:
public class StaticKindOfThing {
static int a;
static int b = 10;
static{
a = getValue();
}
public static int getValue()
{
return b;
}
public static void main (String []args)
{
System.out.println(a);
}
}
Output: 10
The preferred ways to initialize static members are either (as mentioned before)
private static final B a = new B(); // consider making it final too
or for more complex initialization code you could use a static initializer block:
private static final B a;
static {
a = new B();
}
Your code should work. Are you sure you are posting your exact code?
You could also initialize it more directly :
public class A {
private static B b = new B();
A() {
}
void f1() {
b.func();
}
}
If you want to initialize it when the class is loaded, then you should use the static initializer:
public class NewClass {
static int[] arr; //Step 1
static {
arr = new int[10]; //Step 2
for(int i= 0;i<10;i++){
arr[i] = i;
}
}
}
Initializing a static member in the constructor defeats the purpose of static members, since they don't belong to any instance, and each new instance you'll create will override the value of your static array.
You should either initialize the static variable when it's declared or in a static initialization block.
static int[] arr = new int[10];
or
static {
arr = new int[10];
}
The initialization (i.e. the execution of the static declarations and static initialization blocks) will occur when the class is loaded, which happens when your application first accesses any member (constructor, static method, static variable) of your class.
Why the static variables are deterministically initialized and local variables aren't?
See how the static variables are implemented. The memory for them is allocated at link time, and the initial value for them is also provided at link time. There is no runtime overhead.
On the other hand, the memory for local variables is allocated at run time. The stack has to grow. You don't know what was there before. If you want, you can clear that memory (zero it), but that would incur a runtime overhead. The C++ philosophy is "you don't pay for things you don't use", so it doesn't zero that memory by default.
OK, but why are static variables initialized to zero, and not some other value?
Well, you generally want to do something with that variable. But then how do you know if it has been initialized? You could create a static boolean variable. But then it also has to be reliably initialized to something (preferably false). How about a pointer? You'd rather want it initialized to NULL than some random garbage. How about a struct/record? It has some other data members inside. It makes sense to initialize all of them to their default values. But for simplicity, if you use the "initialize to 0" strategy, you don't have to inspect the individual members and check their types. You can just initialize the entire memory area to 0.
This is not really a technical requirement. The semantics of initialization could still be considered sane if the default value is something other than 0, but still deterministic. But then, what should that value be? You can quite easily explain why 0 is used (although indeed it sounds slightly arbitrary), but explaining -1 or 1024 seems to be even harder (especially that the variable may not be large enough to hold that value, etc).
And you can always initialize the variable explicitly.
And you always have paragraph 8.5.6 of the C++ standard which says "Every object of static storage duration shall be zero-initialized at program startup".
For more info, please refer to these other questions:
- Is global memory initialized in C++?
- What do the following phrases mean in C++: zero-, default- and value-initialization?
Paragraph 8.5.6 of the C++ standard states that:
"Every object of static storage duration shall be zero-initialized at program startup"
(The standard also says that the initialization of local variables is undefined)
As to why, the standard doesn't say ;) One guess is that it's reasonably easy to implement without any additional downsides.