After a period of more systematic self-study in Java, it is particularly interesting to see a program that claims more than half of Java programmers will make mistakes. And he went into it.
The program code is as follows:
- Package Com.longpo;
person {
static person person = new person ();
static int count1;
static int count2 = 5;
Person () {
- count1++;
- count2++;
- }
- Public
Static Person getinstance ()
Person
Class Testsingleton {
static void Main (string[] args) {
- Person Person=person.getinstance ();
- You can use direct person.count1
- System.out.println ("Count1:"
+PERSON.COUNT1);
- System.out.println ("Count2:"
+PERSON.COUNT2);
What is the result of the above code output?
It is easy to think that the output 1 and 6 (from the topic is certainly not so simple), then I guess the answer should be 1 and 5, but can not say why. I assign the code to run under Eclipse. Get
As I guessed, but I have no idea why. So began the Google knowledge of the road, after research, ClassLoader gradually reflected into my eyes. Let me say what I think is the reason that there are errors also hope to guide corrections
。
The class performs three steps before executing:
1. Class loading: Find and load the class's binary data, load the corresponding class file into memory
2. Connect
2.1. Verify: Ensure that the class being loaded is correct (primarily to prevent the disgusting class file from being loaded)
2.2. Prepare: Allocate memory for static variables of the class and initialize them to default values
2.3. Parsing: Converting a conforming reference in a class to a direct reference
3. Initialize: Assigns the correct initial value to the static variable of the class
found that steps 2.2 and 3 refer to the keyword static variables, focusing on these two steps, and 2.2 result will result in a static variable
Person,count1,count2 allocating memory and assigning values (default value)
Person=null;
Count1=count2=0
At step 3, the initialization will give the correct value to the static variable, then when will it be initialized? Then
Google Baidu, get:
all Java Virtual machine implementations must be "first actively used by Java programs in each class or interface"
"is initialized only when
Java's use of classes is divided into: active use, passive use
Active use of six kinds: (except for these 6 kinds, the other is passive use)
1. To create an instance of a class
2. Access static variables for a class or interface or assign a value to the static variable
3. To invoke a static method of a class
4. Reflection
5. Initializes subclasses of a class
6. Classes that are labeled to start classes when the Java Virtual machine is started
Understand the third step (initialization), then you can explain why the program output 1 and 5, I drew a diagram
Now you should understand the reason for it, in order to test whether it is really clear, you can change the code to
- Package
Com.longpo;
person {
static int count1;
static int count2 = 5;
static person person = new person ();
Person () {
- count1++;
- count2++;
- }
- Public
Static Person getinstance ()
Person
Class Testsingleton {
static void Main (string[] args) {
- Person Person=person.getinstance ();
- You can use direct person.count1
- System.out.println ("Count1:"
+PERSON.COUNT1);
- System.out.println ("Count2:"
+PERSON.COUNT2);
Original from Techfox Technology Forum Java Community http://techfoxbbs.com/blog-1-3.html
It is said that more than half of Java programmers will make mistakes