JVM 初始化阶段的重要意义分析
1、创建一个Mytest6类和Singleton类
public class MyTest6 {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
System.out.println("counter1:" +Singleton.counter1);
System.out.println("counter2:" +Singleton.counter2);
}
}
class Singleton{
public static int counter1 ;
public static int counter2 = 0;
private static Singleton singleton = new Singleton();
private Singleton(){
counter1 ++;
counter2 ++;
}
public static Singleton getInstance(){
return singleton;
}
}
输出结果
counter1:1
counter2:1
2、将counter2成员变量的位置移动到构造函数后面
public class MyTest6 {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
System.out.println("counter1:" +Singleton.counter1);
System.out.println("counter2:" +Singleton.counter2);
}
}
class Singleton{
public static int counter1 ;
private static Singleton singleton = new Singleton();
private Singleton(){
counter1 ++;
counter2 ++;
System.out.println("Singleton counter1:" +counter1);
System.out.println("Singleton counter2:" +counter2);
}
public static int counter2 = 0;
public static Singleton getInstance(){
return singleton;
}
}
输出结果如下:
Singleton counter1:1
Singleton counter2:1
counter1:1
counter2:0
首先Singleton singleton = Singleton.getInstance(); 是调用Singleton类的getInstance(),属于主动调用。Singleton在准备阶段,按照声明的顺序,赋予所有成员变量默认值。在初始化阶段,构造函数里couonter1和counter2的值变为1,但是后面counter2的值又被赋值为0。 所以打印了上面的结果。
上面代码中的构造函数里counter2 ++;

准备阶段的意义:如果没有准备阶段,counter2是没有值的,更不会有++操作
JVM 初始化阶段的重要意义分析的更多相关文章
- JVM 初始化阶段例子
创建如下Demo package com.example.jvm.classloader; class Parent{ static int a = 3; static { System.out.pr ...
- JVM 初始化阶段例子 final常量
1.创建FinalTest类,里面有一个final常量x class FinalTest{ public static final int x = 3; static { System.out.pri ...
- SpringMVC初始化阶段流程源码分析
1.都知道SpringMVC项目启动的时候都会初始化一个类:DispatcherServlet,看这个类的源码我们可以发现他其实就是一个servlet, 为什么这么说呢?请看: DispatcherS ...
- JVM调优总结 + jstat 分析(转)
[转] JVM调优总结 + jstat 分析 JVM调优总结 + jstat 分析 jstat -gccause pid 1 每格1毫秒输出结果jstat -gccause pid 2000 每格2秒 ...
- junit源码解析--初始化阶段
OK,我们接着上篇整理.上篇博客中已经列出的junit的几个核心的类,这里我们开始整理junit完整的生命周期. JUnit 的完整生命周期分为 3 个阶段:初始化阶段.运行阶段和结果捕捉阶段. 这篇 ...
- Jvm threaddump,heapdump的分析及问题定位
1 一.Thread Dump介绍 1.1 1.1什么是Thread Dump? 1.2 1.2 Thread Dump特点 1.3 1.3 Thread Dump 能诊断的问题 1.4 1.4如何抓 ...
- u-boot分析(九)----nand flash初始化|nand flash读写分析
u-boot分析(九) 上篇博文我们按照210的启动流程,分析到了初始化串口,由于接下来的取消存储保护不是很重要,所以我们今天按照u-boot的启动流程对nand flash初始化进行分析. 今天我们 ...
- 深入理解JVM-类加载初始化阶段-类的主动与被动引用
JVM的类加载阶段中初始化阶段 P210 虚拟机规定的五种情况必须对类的“初始化”情况 1.遇到new.getstatic.putstatic.或invokestic 四条字节码指令时,如果类没有经过 ...
- 一起学习vue源码 - Vue2.x的生命周期(初始化阶段)
作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/58c61b4361ff4b005d9e8 ...
随机推荐
- centos7 安装 git服务器
服务器端配置 yum install -y git groupadd git useradd git -g git 2.创建authorized_keys cd /home/git mkdir .ss ...
- 使用Cloudera Manager搭建zookeeper集群及HDFS HA实战篇
使用Cloudera Manager搭建zookeeper集群及HDFS HA实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用Cloudera Manager搭建zo ...
- SQL 执行 底层原理(一)
一.SQL Server组成部分 1.关系引擎:主要作用是优化和执行查询.包含三大组件: (1)命令解析器:检查语法和转换查询树. (2)查询执行器:优化查询. (3)查询优化器:负责执行查询. 2. ...
- c#版本23个设计模式
一.引言 对设计模式的学习,自己的感触还是很多的,因为我现在在写代码的时候,经常会想想这里能不能用什么设计模式来进行重构.所以,学完设计模式之后,感觉它会慢慢地影响到你写代码的思维方式.这里对设计模式 ...
- 铁力项目mysql异常处理过程记录
地区:铁力 故障:2019-06-26 10:19:34 139921514837760 [ERROR] mysqld: Error writing file 'mysql-bin' (errno: ...
- @ConfigurationProperties绑定配置信息至Array、List、Map、Bean
原文:https://blog.csdn.net/justry_deng/article/details/90758250 相关说明:在SpringBoot中,我们可以通过以下几种方式获取并绑定配置文 ...
- 个性化排序算法实践(四)——GBDT+LR
本质上GBDT+LR是一种具有stacking思想的二分类器模型,所以可以用来解决二分类问题.这个方法出自于Facebook 2014年的论文 Practical Lessons from Predi ...
- mysql-proxy读写分离笔记
1.MySQL的安装与配置 --省略 版本:Server version: 5.6.35 1.1 系统版本: [root@centos7-67 package]# lsb_release -a LSB ...
- Union-Find(并查集): Quick union算法
Quick union算法 Quick union: Java implementation Quick union 性能分析 在最坏的情况下,quick-union的find root操作cost( ...
- H5性能测试,首屏时间统计(Argus)
Argus 腾讯质量开发平台,官网链接:https://wetest.qq.com/product/argus 主要针对性:H5的游戏性能测试 主要介绍: 独家首屏时间统计: 告别人工掐秒 自动统计首 ...