Java Variable initialization

Source: Internet
Author: User

The source code becomes a running program that needs to be compiled--and loaded--to run several stages.

Final-Modified variables must be explicitly initialized. There are three ways of initializing:

(1) Direct initialization

(2) Dynamic code block

(3) Constructors

If a variable is both final and static, the variable must be initialized (to satisfy the final attribute), or it can be initialized either directly or statically

Initialization in a code block (satisfies the static attribute).

The initialization of a final modified variable precedes the static modified variable, and the static modified variable precedes the initialization of the normal variable.

If it is final, it is initialized in the order of the Code, but it is a macro substitution at compile-time, that is, the value is known during the compilation phase.

Therefore, it is not fully aware of this sequencing during the load run phase.

However, if the static level, the order of the Code will cause the program to run different results. Because they are initialized when the load is in progress.

It boils down to the following:

(1) Final modified variables are completed in the compilation phase

(2) Static modified variables are initialized in a static code block.

(3) The normal variable is initialized in the constructor function.

1 classPrice {2     Final StaticPrice instance=NewPrice (2.8);3     Final Static DoubleInitprice=20;4     DoubleCurrentprice;5      PublicPrice (Doublediscount) {6currentprice=initprice-discount;7     }8 }9 Ten  Public classTest { One      Public Static voidMain (String[]args) { A System.out.println (Price.INSTANCE.currentPrice); -     } -}

Run Result: 17.2

1 classPrice {2     StaticPrice instance=NewPrice (2.8);3     Static DoubleInitprice=20;4     DoubleCurrentprice;5      PublicPrice (Doublediscount) {6currentprice=initprice-discount;7     }8 }9 Ten  Public classTest { One      Public Static voidMain (String[]args) { A System.out.println (Price.INSTANCE.currentPrice); -     } -}

Run Result: 2.8

1 classPrice {2     Static DoubleInitprice=20;3     StaticPrice instance=NewPrice (2.8);4     DoubleCurrentprice;5      PublicPrice (Doublediscount) {6currentprice=initprice-discount;7     }8 }9 Ten  Public classTest { One      Public Static voidMain (String[]args) { A System.out.println (Price.INSTANCE.currentPrice); -     } -}

Run Result: 17.2

1 classPrice {2Price instance=NewPrice (2.8);3      PublicPrice (Doublediscount) {4         5     }6 }7 8  Public classTest {9      Public Static voidMain (String[]args) {Ten         NewPrice (29); One     } A}

Run result: Exception in thread "main" Java.lang.StackOverflowError

Program Analysis and comparison:

These procedures, in fact, are to derive the result of the Currentprice variable, which is a normal variable, so it is initialized in the constructor. To conclude that Currentprice had to have instance first,

Instance is an instance of price, and the constructor must be called first to have instance. The constructor will look at the initialization values of the Initprice and discount variables at this time.

For programs (1) because the variables are final decorated, the order in which the code is initialized is consistent for the code that loads the runtime.

For programs (2) because variables are modified by static, and because the code order of the variable instance precedes Initprice, the instance initialization precedes Initprice, which is visible by the runtime code.

So when the program initializes the initialization of the instance,instance requires Initprice and discount two variables, and the Initpirce variable is not explicitly initialized by the code, but the system is initialized, so

At this point the value of Initprice is 0. So the result is-2.8.

For a program (3) variable is also modified by static, the code order of the variable initprice precedes the variable instance, so the initialization of the Initprice precedes the instance initialization. So when the program needs to get the value of Currentprice,

The instance variable needs to be called, and the instance variable then looks at the value of Initprice, which is initialized so that the result is 17.2.

For programs (4) When we new instance, we call the constructor to initialize the instance variable. The initialization of a common variable is achieved by taking it to the constructor. The equivalent of the code above becomes:

1 class Price {2      Public Price (double  discount) {3         -instance=new Price (2.8); 4     }5 }

The constructor is called outside, and the constructor calls itself. Therefore, the constructor has been called and has been pushed into the stack. Cause a stack overflow.

The result of the above program occurs during the code load phase, so there is a problem with the static code sequencing, and if the code is called by an instance, the initialization order inconsistency caused by different code order is not visible. that for

The code initialization of the instance invocation phase is consistent. As is, final is consistent with the load phase.

Java Variable initialization

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.