jvm五:编译期不能确定常量的值

package com.atChina.jvm;

import java.util.UUID;
/*
 *   当一个常量的值并非编译期间可以确定的,那么其值就不会被放到调用类的常量池中
 *   这时在程序运行时,会导致主动使用这个常量所在的类,因此常量所在的类会被初始化
 */
public class Test3 {
    public static void main(String[] args) {
        System.out.println(Father3.f); // Father3会被初始化,因此Father3中的静态代码块的代码会被执行
    }
}

class Father3{
    public static final String f = UUID.randomUUID().toString(); //  编译期间,该常量值不能确定

    static{
        System.out.println("桃园结义");
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37564426/article/details/89042146
今日推荐