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 初始化阶段的重要意义分析的更多相关文章

  1. JVM 初始化阶段例子

    创建如下Demo package com.example.jvm.classloader; class Parent{ static int a = 3; static { System.out.pr ...

  2. JVM 初始化阶段例子 final常量

    1.创建FinalTest类,里面有一个final常量x class FinalTest{ public static final int x = 3; static { System.out.pri ...

  3. SpringMVC初始化阶段流程源码分析

    1.都知道SpringMVC项目启动的时候都会初始化一个类:DispatcherServlet,看这个类的源码我们可以发现他其实就是一个servlet, 为什么这么说呢?请看: DispatcherS ...

  4. JVM调优总结 + jstat 分析(转)

    [转] JVM调优总结 + jstat 分析 JVM调优总结 + jstat 分析 jstat -gccause pid 1 每格1毫秒输出结果jstat -gccause pid 2000 每格2秒 ...

  5. junit源码解析--初始化阶段

    OK,我们接着上篇整理.上篇博客中已经列出的junit的几个核心的类,这里我们开始整理junit完整的生命周期. JUnit 的完整生命周期分为 3 个阶段:初始化阶段.运行阶段和结果捕捉阶段. 这篇 ...

  6. 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如何抓 ...

  7. u-boot分析(九)----nand flash初始化|nand flash读写分析

    u-boot分析(九) 上篇博文我们按照210的启动流程,分析到了初始化串口,由于接下来的取消存储保护不是很重要,所以我们今天按照u-boot的启动流程对nand flash初始化进行分析. 今天我们 ...

  8. 深入理解JVM-类加载初始化阶段-类的主动与被动引用

    JVM的类加载阶段中初始化阶段 P210 虚拟机规定的五种情况必须对类的“初始化”情况 1.遇到new.getstatic.putstatic.或invokestic 四条字节码指令时,如果类没有经过 ...

  9. 一起学习vue源码 - Vue2.x的生命周期(初始化阶段)

    作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/58c61b4361ff4b005d9e8 ...

随机推荐

  1. Springboot默认定时任务——Scheduled注解

    1.pom配置 <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  2. UEditor 在 Layer 模态框中无法使用问题

    问题: 解决方法: 在 使用  ueditor 的页面顶部加入js代码: window.UEDITOR_HOME_URL = "__STATIC__/path/to/ueditor/&quo ...

  3. Flask之WTfroms组件

    一.WTfroms简介 WTForms插件是类似于django的form组件的插件,可以帮我们写标签,校验数据等. 二.安装与使用 安装: pip install WTForms 使用: from w ...

  4. containerd简述

    containerd是容器虚拟化技术,从docker中剥离出来,形成开放容器接口(OCI)标准的一部分. docker对容器的管理和操作基本都是通过containerd完成的.Containerd 是 ...

  5. Failed to close the ServletOutputStream connection cleanly, Broken pipe

    Problem1: 服务端报错:Broken pipejava.io.IOException: Connection timed out at sun.nio.ch.FileDispatcherImp ...

  6. 【转】以Python为例的Async / Await的编程基础

    转, 原文:https://www.cnblogs.com/middleware/p/11996731.html 以Python为例的Async / Await的编程基础 -------------- ...

  7. python制作的翻译器基于爬取百度翻译【笔记思路】

    #!/usr/bin/python # -*- coding: cp936 -*- ################################################### #基于百度翻 ...

  8. vm.$attrs与inheritAttrs详解

    1. inheritAttrs 在vue官网的解释如下 个人理解:父组件A上引入子组件B,在B子组件上加上一些属性(class.style除外),这些属性能否在子组件B的根元素上继承,默认值为true ...

  9. Oracle 序号函数

    Oracle提供的序号函数:以emp表为例:1: rownum 最简单的序号 但是在order by之前就确定值.select rownum,t.* from emp t order by ename ...

  10. P4462 [CQOI2018]异或序列 莫队

    题意:给定数列 \(a\) 和 \(k\) ,询问区间 \([l,r]\) 中有多少子区间满足异或和为 \(k\). 莫队.我们可以记录前缀异或值 \(a_i\),修改时,贡献为 \(c[a_i\bi ...