It is said that more than half of Java programmers will make mistakes

Source: Internet
Author: User

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:

    1. Package Com.longpo;
    • Class

person {

    • Private

static person person = new person ();

    • Public

static int count1;

    • Public

static int count2 = 5;

    • Private

Person () {

    • count1++;
    • count2++;
    • }
    • Public

Static Person getinstance ()

    • {
    • Return

Person

    • }
    • }
    • Public

Class Testsingleton {

    • Public

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

    1. Package

Com.longpo;

    • Class

person {

    • Public

static int count1;

    • Public

static int count2 = 5;

    • Private

static person person = new person ();

    • Private

Person () {

    • count1++;
    • count2++;
    • }
    • Public

Static Person getinstance ()

    • {
    • Return

Person

    • }
    • }
    • Public

Class Testsingleton {

    • Public

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.