Java initialization field (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/11/30/2796666.html

As you can see, we often provide an initialization value in the field declaration:

 
Public ClassBedandbreakfast {//Initialize to 10Public Static IntCapacity = 10;//Initialize to falsePrivate BooleanFull =False;}

When the initialization value is available, the initialization statement is also placed in one row, the aboveCodeYou can work. However, this form of initialization is relatively simple, so there are some restrictions. If initialization requires some logic processing (for example, error processing or filling a complex array through loops), simple assignment is insufficient. Instance variables can be initialized in the constructor, and error processing and logical processing can be performed. To provide the same functions for class variablesProgramThe language contains a static initial block.

Note: Although declaring a field is the most common practice at the beginning of a class declaration, it is not required. It must be declared and initialized before use.

 

Static initial block

A static initialization code block, like a normal code block, is closed using braces {}. There is a static keyword in front. Here is an example:

 
Static{//Whatever code is needed for initialization goes here}

A class can have any number of static initial blocks and appear anywhere in the class body. The operating system ensures the calling sequence of the static initial block andSource codeThe order of appearance is consistent.

 

Here is an alternative code block method-you can write a Private Static Method:

ClassWhatever {Public StaticVartype myvar =Initializeclassvariable ();Private StaticVartype initializeclassvariable (){//Initialization code goes here}}

The advantage of the private static method is that it can be reused when you need to reinitialize the variable.

 

Instantiate an instance Member

In general, you will put the initialization code of the variable in the constructor. There are two optional methods to initialize the instance variables using the constructor: the initialization code block and the final method.

The initialization code block of instance variables is similar to the static initialization code block, but there is no static Keyword:

{//Whatever code is needed for initialization goes here}

The Java compiler copies the initialization code block to each constructor. Therefore, this method can be used to share code blocks in multiple constructor methods.

A final method cannot be overwritten by the quilt class. This will be discussed in the interface and inheritance sections. Here is an example of using the final method to initialize instance variables:

 
ClassWhatever {PrivateVartype myvar =Initializeinstancevariable ();Protected FinalVartype initializeinstancevariable (){//Initialization code goes here}}

This is particularly useful if the subclass needs to reuse the initialization code. The initialization code is set to final, Which is because non-final method is called during instance initialization.

Related Article

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.