常量非在编译期间可以确定的值

package com.atzhangwl.jvm.classloader;

import java.util.UUID;

/**
* @ClassName Run_03
* @Description
* @Author zhangwl
* @Date 2020/1/4 16:36
* @Version 1.0
**/
public class Run_03 {

public static void main(String[] args) {
System.out.println(MyParent3.parentStr);
}
}


class MyParent3 {

/**
*当一个常量并非编译期间可以确定的值时,那么该常量值就不会被放在调用类的常量池中,
*在程序运行时,会导致主动使用这个常量所在的类,从而导致这个类被初始化
*/
public static final String parentStr = UUID.randomUUID().toString();
// public static final String parentStr = "parentStr";

static {
System.out.println("The static block is from parent");
}
}

猜你喜欢

转载自www.cnblogs.com/sico/p/12150567.html