Static and final modifier class property variables and initialization

Source: Internet
Author: User

1. if a property field is modified in static mode, this property field becomes a resource of the class, and the public field is modified to a common one. You can use test outside the class. a. if it is modified to private, it can only be used within the class.

Public class test {
Public static int;
Private test (){
A = 0;
}
}

If the attribute is modified as a static class resource, this field will always have only one, that is, no matter how many class objects you new test, the operation is always the memory resource of the class. for example:

Test T1 = new test ();
T1.a = 10;
Test t2 = new test ();
System. Out. println (t1.a );
System. Out. println (t2.a );
System. Out. println (test. );

The result is 3 0.

2. final is used to declare attributes. Methods and classes indicate that attributes must be initialized once memory space is allocated and will not be changed in the future. Once defined, methods must have implementation code and cannot be overwritten in subclasses, once defined, a class cannot be defined as an abstract class or interface, because it cannot be inherited.

The final Attribute Modified in your code is modified, so it is incorrect.

3. attribute variables of classes modified by final but not static can only be initialized in two cases:

A. When it is defined, for example:

Public class test {
Public final int A = 0;
Private test (){
}
}

B. initialize In the constructor, for example:

Public class test {
Public final int;
Private test (){
A = 0;
}
}

4. attribute variables of classes simultaneously modified by final and static can only be initialized under two conditions:

A. When it is defined, for example:

Public class test {
Public final int A = 0;
Private test (){
}
}

B. initialize the static block of the class, for example:

Public class test {
Public final int;
Static {
A = 0;
}
}

5. Analyze the third and fourth reasons:

Article 3: When this attribute is modified to final instead of static, it belongs to the resources of the class instance object, when a class is loaded into the memory, this attribute does not allocate memory space to it, but defines a variable, this attribute is allocated memory space only when the class is instantiated, And the constructor is executed at the time of instantiation, so the attribute is initialized, it also meets the conditions that need to be initialized when it is allocated memory space and will not be changed later.

Article 4: When a class attribute is modified to static and final at the same time, it belongs to the class resource, when the class is loaded into the memory (that is, when the application starts), the memory will be allocated for this attribute. Therefore, the attribute already exists and is modified by final, therefore, the attribute must be initialized after it is defined. the constructor is executed only when the class is instantiated. Therefore, this attribute is not initialized when the constructor is used. the program reports an error. the static block is executed only when the class is loaded. Therefore, it can be initialized in the static block.

Recommended for beginners a programming technology learning site, 96 stack Software Programming Network, http://www.96dz.com, which has c ++ video tutorial, C # video tutorial, Java video tutorial download, c \ c ++, Java, and C #. net and other programming technology abstracts, including the current mainstream Linux programming and web programming learning materials video tutorial download.

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.