Here’s a very basic application in Java, containing only one class
. In that class, there is one main
method and two static
blocks.
class Test {
public static void main(String args[]) {
System.out.println("Main");
}
static {
int a = 10;
}
static {
int a = 20;
}
}
And here’s the bytecode produced by compiling this application. I don’t understand what has happened with the static blocks:
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=1, args_size=0
0: bipush 10
2: istore_0
3: bipush 20
5: istore_0
6: return
LineNumberTable:
line 34: 0
line 37: 3
line 38: 6
My question is: where is the second static block? If they merge, then how can the JVM differentiate between variables contained by both blocks, because both blocks have variables with same name and type?