当前位置:网站首页>A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
2022-06-28 09:22:00 【A bowl of humble powder】
One 、 subject
Almost the same code , There are two cases :
Situation 1 :new SingleTon() rearwards , And what you get is :count1 = 1 and count2 = 1

Situation two :new SingleTon() in front , And what you get is :count1 = 1 and count2 = 0

【 ask 】 Please give us a brief introduction , Why the output results of these two cases are different ?
Two 、 analysis
The main knowledge involved here is ,JVM Class loading mechanism of .
1、 The life cycle of a class : load -> verification -> Get ready -> analysis -> initialization -> Use -> uninstall , And verification , Preparation and parsing can be classified as the connection phase .
2、 When calling a static method in a class getSingleTon() when , Will trigger class initialization .
(1) For case one :

1) Connection phase , Will assign initial values to static variables , At this time count1=0,count2=0;
2) Then the initialization phase , Code execution from top to bottom , Assignment operations and static code blocks , At this time count1=0,count2=0;
Create objects SingleTon after , Yes count1 and count2 Conduct ++, Last count1=1,count2=1.
(2) For case two :

1) Connection phase , It is also a static variable that gives an initial value , It's the same time count1=0,count2=0;
2) Initialization phase , Execute code from top to bottom , Create the object first , Yes count1 and count2 Conduct ++, At this time count1=1,count2=1; then public static int count1 Yes count1 There is no assignment , At this time count1 Or is it equal to 1, however count2 Assigned a value to 0, At this time count2=0, So the final output count1=1,count2=0
【 Code 】
class SingleTon{
private static SingleTon singleTon = new SingleTon();
public static int count1;
public static int count2 = 0;
private SingleTon(){
count1++;
count2++;
}
public static SingleTon getSingleTon(){
return singleTon;
}
}
public class ClassLoaderTest {
public static void main(String[] args) {
SingleTon.getSingleTon();
System.out.println("count1 = "+ SingleTon.count1);
System.out.println("count2 = "+ SingleTon.count2);
}
}边栏推荐
- Summary of PMP learning experience
- 理解IO模型
- Dbeaver连接人大金仓KingbaseES V8(超详细图文教程)
- Do static code blocks always execute first? The pattern is smaller!!!
- 1181:整数奇偶排序
- Fire safety hazards
- High rise building fire prevention
- Implementation of single sign on
- 1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
- How to implement two factor authentication MFA based on RADIUS protocol?
猜你喜欢

Discussion on safety management of centralized maintenance construction site of substation under the mode of operation and maintenance

Import and export of a single collection in postman

Discussion on the improvement and application of the prepayment system in the management of electricity charge and price

Characteristics and prevention of electrical fire

1182:合影效果

Apache Doris 成为 Apache 顶级项目

虛擬機14安裝win7(圖教程)

Fire fighting work and measures in Higher Vocational Colleges

Prototype chain JS

Rich text - Test Case
随机推荐
Fire fighting work and measures in Higher Vocational Colleges
DEJA_VU3D - Cesium功能集 之 052-模拟卫星轨道(高空)效果
Implementation of single sign on
What is online account opening? Is it safe to open an account online now?
Discussion on the improvement and application of the prepayment system in the management of electricity charge and price
使用transform:scale之后导致页面鼠标悬浮事件消失
DEJA_ Vu3d - 051 of cesium function set - perfect realization of terrain excavation
State machine program framework
Linux下安装redis 、Windows下安装redis(超详细图文教程)
Zhejiang energy online monitoring and management system
[share OpenGL tutorial]
Quickly understand JVM structure and working principle
学习阿里如何进行数据指标体系的治理
STL -- binder
虚拟机14安装win7(图教程)
STL - inverter
Loggerfactory uses log4j Parameter introduction of properties
How to solve the problem of port number occupation
Protection range and optimization of motor protector for hoist equipment
"Jianzhi offer" -- Interview Question 4: finding two-dimensional arrays