Reputation: 407
I have few question about static in java
Can we have static block in non static class??If an object of that(no-static) class is initialized in some other class,will the static block get executed then??
If there is a static class having static block and variable( int a = 3) and main() method,and it is executed then will the variable initialization take place first or static block execution?I know that static block gets executed before the main() method.
Upvotes: 1
Views: 3475
Reputation: 8842
Please look here http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
It's called static initializer in the spec. This code will be executed when JVM loads the class. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.
You can't use static code in nonstatic inner classes:
public class MyClass {
static {
System.out.println("static code from nonstatic class");
}
class In {
/* it will not compile
static {
}
*/
}
static class In2 {
static {
System.out.println("static code from static inner class");
}
}
public static void main(String[] args) {
MyClass c = new MyClass();
}
}
See Detailed Initialization Procedure in The Java® Language Specification for details about calling order. Simply the static blocks are executed in the order they appear in the source code.
Upvotes: 3
Reputation: 122364
For question 2, the answer can be found in the Java Language Specification §12.4.2, in particular step 9 of the "detailed initialization procedure":
Next, execute [...] the class variable initializers and static initializers of the class [...] in textual order, as though they were a single block.
In other words, the static {}
blocks and the initializer expressions of static fields are executed in the order they appear in the source code - if the static block is before the field declaration then it will run first (and see the default value of the field, typically null
or 0), if the static block is after the field declaration then it will run second (and will see the value assigned by the initializer expression).
Upvotes: 1
Reputation: 136002
1) No, you cant, try this
class Test1 {
class X {
static { <-- compile error: Cannot define static initializer in inner type Test1.X
}
}
...
X should be static.
Note: I assume we are talking about nested classes, because this is where static modifier is applicable for classes
2) it depends, see this
class Test1 {
static {
x = 3;
}
static int x = 2;
public static void main(String[] args) throws Exception {
System.out.println(x);
}
}
output
2
but now
class Test1 {
static int x = 2;
static {
x = 3;
}
public static void main(String[] args) throws Exception {
System.out.println(x);
}
}
output will be
3
Static initialization runs only ones during class loading, it happens always before any instance instantiation. Interestingly both static fields initialization and static init block code runs in the same bytecode function with the name <clinit>
:
static <clinit>()V
L0
LINENUMBER 12 L0
ICONST_3
PUTSTATIC test/Test1.x : I
L1
LINENUMBER 15 L1
ICONST_2
PUTSTATIC test/Test1.x : I
RETURN
Upvotes: 4
Reputation: 13556
Yes we can have a static block in a non-static
class and it is loaded as soon as class loads in JVM
public class StaticDemo {
static{
System.out.println("already loaded");
}
public static void main(String [] args){
}
}
This outputs already loaded
Upvotes: 1
Reputation: 5373
Having a static class vs. a normal class does not affect what programming constructs you are allowed to use, but simply controls whether or not the class has access to instance fields/methods of the surrounding class.
Upvotes: 1