/**
* 常量在编译阶段会存入到调用这个常量的方法所在的类的常量池中,
* 本质上,调用类并没有直接引用到定义常量的类,因此并不会触发
* 定义常量的类的初始化
* 注意:这个指的是将常量存放到了Test的常量池中,之后Test与MyParent就没有任何关系了
* 甚至,我们可以将MyParent的class文件删除
*/
public class Test {
public static void main(String[] args) {
System.out.println(MyParent.str);
}
}
class MyParent{
public static final String str = "hello world";
static {
System.out.println("MyParent static block");
}
}
调用常量类是否初始化
猜你喜欢
转载自blog.csdn.net/qq_40807366/article/details/103560063
今日推荐
周排行