When I tried to write something like this:
public interface MyInterface {
static {
System.out.println("Hello!");
}
}
the compiler could not compile it.
But when I wrote something like this:
interface MyInterface {
Integer iconst = Integer.valueOf(1);
}
and decompiled it, I saw static initialization:
public interface MyInterface{
public static final java.lang.Integer i;
static {};
Code:
0: iconst_1
1: invokestatic #1; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
4: putstatic #2; //Field i:Ljava/lang/Integer;
7: return
}
Could you please explain this behavior to me?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…