当前位置:网站首页>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);
}
}边栏推荐
- Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes
- Calculation of stock purchase and sale expenses
- 1181:整数奇偶排序
- Android studio interview preparation
- Apache Doris 成为 Apache 顶级项目
- Test cases for learning the basic content of software testing (II)
- Applet: traverse the value of an array in the list, which is equivalent to for= "list" list An item in comment
- 股票 停牌
- Is it safe to open an account for online stock speculation?
- RESTful风格
猜你喜欢

new URL(“www.jjj.com“)

自动转换之-面试题

SQL injection file read / write

Application of current limiting protector in preventing electrical fire in shopping malls

1182:合影效果
![1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation

Full link service tracking implementation scheme

从知识到智慧:知识图谱还要走多远?

Matlab tips (20) matrix analysis -- principal component regression

Discussion on the improvement and application of the prepayment system in the management of electricity charge and price
随机推荐
The constructor is never executed immediately after new()!!!!!
The private attribute of this class can be used directly? New() in use!!!
Illustration of MySQL binlog, redo log and undo log
Implementation of single sign on
Music website design based on harmonyos (portal page)
Play SFTP upload file
[share OpenGL tutorial]
Why does select * lead to low query efficiency?
Calculation of stock purchase and sale expenses
new URL(“www.jjj.com“)
Linux下安装redis 、Windows下安装redis(超详细图文教程)
APICloud携手三六零天御,助力企业守好App安全“第一关”
手机炒股开户安不安全?
Which occupational groups are suitable for the examination
学习阿里如何进行数据指标体系的治理
1182: effets de la photo de groupe
Static page of pinyougou mall
Valentine's Day - VBS learning (sentences, love words)
我想网上注册股票开户,如何操作?在线开户安全么?
How to solve the problem of port number occupation