本文共 1742 字,大约阅读时间需要 5 分钟。
/* * Copyright (c) 2019 maoyan.com * All rights reserved. * */import java.util.UUID;/** * 当一个常量的值并非编译器可以确定的时候,那么其值就不会放到调用类的常量池里 * 这个时候程序运行的时候,会导致主动使用这个常量所在的类,显然会导致这个类被初始化 * * @author wangkai * @created 2019/12/27 */public class MyTest2 { public static void main(String[] args) { System.out.println(MyParent2.str); }}class MyParent2 { public static final String str= UUID.randomUUID().toString(); static { System.out.println("MyPatents static code"); }}
/* * Copyright (c) 2019 maoyan.com * All rights reserved. * *//** * * 对于数组实例来说,其类型是由JVM在运行期动态生成的,表示为[LMyParent3 这种形式,动态生成的类型,其父类型就是Object * 对于数组来说,JavaDoc经常将构成数组的元素为Component,实际上就是讲数组降低一个维度后的类型 * * 反编译(javap -c)后看助记符: * anewarray:表示创建一个引用类型的(如类、接口、数组)数组,并将其引用值压人栈顶 * newarray: 表示创建一个指定的原始类型(如int、float、char等)的数组,并将其引用值压入栈顶 * * @author wangkai * @created 2019/12/27 */public class MyTest3 { public static void main(String[] args) { //首次new对象会触发类的初始化 //MyParent3 myParent3=new MyParent3(); //MyParent3 myParent4=new MyParent3(); //数组会不会触发类的初始化 MyParent3[] myParents=new MyParent3[1]; System.out.println(myParents.getClass()); System.out.println(myParents.getClass().getSuperclass()); System.out.println("============"); MyParent3[][] myParent3s=new MyParent3[1][1]; System.out.println(myParent3s.getClass()); System.out.println(myParent3s.getClass().getSuperclass()); System.out.println("============"); int[] ints=new int[1]; System.out.println(ints.getClass()); System.out.println(ints.getClass().getSuperclass()); }}class MyParent3{ static { System.out.println("static parent"); }}
转载地址:http://vobjn.baihongyu.com/