Turn: A test of Java----static variable initialization process

Source: Internet
Author: User

 Public classtest{Private StaticTest tester =NewTest ();//Step 1    Private Static intCount1;//Step 2    Private Static intCount2 = 2;//Step 3     PublicTest () {//Step 4count1++; Count2++; System.out.println ("" + Count1 +Count2); }     Public StaticTest Gettester () {//Step 5        returntester; }         Public Static voidMain (string[] args) {test.gettester (); }}

Q: The order of the above code execution ~, the result of the output ~

Positive solution:

Depending on the nature of the static object, the program's execution flow is:

Test tester = null;
int count1 = 0;
int count2 = 0;

Tester = new Test ();

Count1 + +;
Count2 + +;
Output 1 1

Count2 = 2;
The final result is 1 2 and the output is 1 1

1,JVM Virtual machine startup is done by loading an initialization class with the bootloader (Bootstrap class Loader), which is specified by the specific implementation of the virtual machine, which is the generic startup class (The main Class), and then the virtual machine links the class, initializes and invokes its public void Main (string[]) method.

2, in the interview question, according to the context can be thought that the initialization class is the test class, so:
A, first load the class, and then in the preparation phase of the link (link includes validation, preparation, referencing three stages), allocate memory for all class (static) variables, set to default (Test tester = null; int count1 = 0; int count2 = 0;)

b, after the link is complete, Initialize (Class), Initialize the class (static) variable in the order declared in the code, that is, the first call

Java Code
private static Test Tester = new test (); //Step 1


Note: The base class initialization and <clinit> related details are omitted here.

C, the new in the above step triggers the instantiation of the test class (object creation), allocates memory on the heap, then sets the object variable (not in this case) as the initial value, and then calls <init>

Java Code
Public
Test () { //Step 4 count1+ +; Count2+ +; System.out.println ("" + count1 + count2);}


  Obviously, the count1 and Count2 are not initialized at this time, but they are simply set to the default value of 0 (during the preparation phase of the link). So the printed value is always 11.

  D, next proceed to initialize in the order of Declaration, that is:

Java code
private static int Count1; //step 2 private static int Count2 = 2//step 3



E, after initialization is complete, the initial class is loaded and jumps to the main method to begin execution.

So the order is 14253, and no matter how much count2 and Count1, the print out is always 11. If you exchange the order, for example

Java Code
Count 2 = 2; new test ();



Then the result of the printing will be 13

Turn: A test of Java----static variable initialization process

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.