接口中的静态初始化
- 论坛
- 接口中的静态初始化
18 浏览
接口中的静态初始化
当我尝试写下这样的代码时:
public interface MyInterface { static { System.out.println("Hello!"); } }
编译器无法编译它。
但是当我写下这样的代码时:
interface MyInterface { Integer iconst = Integer.valueOf(1); }
并对其进行反编译,我看到了静态初始化:
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 }
你能解释一下这种行为吗?