JVM 类的加载(二)- 类的主动加载

类的加载有六种主动方式

  • 创建类的实例
  • 访问某个类或接口的静态变量,或者对该静态变量赋值
  • 调用类的静态方法
  • 反射
  • 初始一个类的子类(相反初始化父类不会初始子类
  • Java虚拟机启动时被标明为启动类的类

通过下面的例子,验证类的启动方式

class SingletonOne {
    private static SingletonOne singletonOne = new SingletonOne();
    public static int counter1;
    public static int counter2 = 0;
    private SingletonOne() {
        counter1++;
        counter2++;
    }
    public static SingletonOne getSingletonOne() {
        return singletonOne;
    }
}

class SingletonTwo {
    public static int counter1;
    public static int counter2;
    private static SingletonTwo singletonTwo = new SingletonTwo();
    private SingletonTwo() {
        counter1++;
        counter2++;
    }
    public static SingletonTwo getSingletonTwo() {
        return singletonTwo;
    }
}

public class TestClassLoaderOrder {
    public static void main(String[] args) {
        System.out.println("singletonOne count1 = " + SingletonOne.counter1);
        System.out.println("singletonOne count2 = " + SingletonOne.counter2);

        System.out.println("singletonTwo count1 =" + SingletonTwo.counter1);
        System.out.println("singletonTwo count2 =" + SingletonTwo.counter2);
    }
}

以上代码,SingletonOne和SingletonTwo 两个类的代码唯一的区别就是

public static int counter1;

public static int counter2;

private static SingletonTwo singletonTwo = new SingletonTwo();

两段代码的顺序,但是就是不同的顺序产生不同的结果

singletonOne count1 = 1

singletonOne count2 = 0

singletonTwo count1 =1

singletonTwo count2 =1

SingletonTwo 容易理解,下面来探讨一个为什么SingletonOne 里的count2为什么会是“0” 由文章开头所说,访问某个类或接口的静态变量,或者对该静态变量赋值就会对类进行加载,在System.out.println()里面均访问了类的静态变量,所以两个类都会进行加载。 SingletonOne 类在启动时,会经历,加载,连接,初始化的阶段,连接的时候,也分三步,验证,准备,解析,在以前博文有提过https://my.oschina.net/u/1273644/blog/768476 准备的时候,会给静态变量初始化默认值,这时首选counter1和counter2都是给默认值0 此时

counter1 = 0;

counter2 = 0;

紧接着进入类的初始化阶段,第一段代码执行调用了构造方法两个值均相加1

counter1 = 1;

counter2 = 1;

在下面,count1只是声明,不会对值改变,而count2会给此变量重新初始化值为0

counter1 = 1;

counter2 = 0;

最后得出我们想要的结果。

SingletonTwo相同的方式来验证,不做赘述。

转载于:https://my.oschina.net/u/1273644/blog/769779

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值