Static/transient,final/volatile description in Java

Source: Internet
Author: User
Tags volatile

You can define a field using any of the following modifier-qualified keywords: final or volatile and/or static and/or transient. If you define a field as final, the compiler will ensure that the field is initialized and processed as a constant-read-only variable. Because the compiler knows that constants are immutable, it is internally optimized in the program's bytecode. classemployee{finalintACCOUNTANT =1; finalintPayroll_clerk =2; finalintMANAGER =3;intJobID =ACCOUNTANT;} If you declare a field to be volatile, multi-threading will be able to access this field, and a particular compiler will prevent optimization so that the field is properly accessed. If you define a field as static, all objects will share a copy of this field. When you assign a new value to this field, all objects will get the new value. If it is not specified as static, this field will be an instance field, with each object using one copy of their own. field values defined as transient are not saved during the serialization of an object. Instance field "Instance field" is a field that is not defined with the static modify identifier. Instance fields are closely connected to objects--not to and classes. When modified in an object code, only the relevant class instance-object-can get this change. Instance fields are created with the creation of the object and released as the object is released. The Class field Class field is a field that is defined with the static keyword. Class fields are associated with classes-not objects. When modified in a class code, this class (and all created objects) can perceive this change. Class fields are created with the load of the class, and the constant "constant" is a read-only variable that is released with the unload of the class, and the value of the variable cannot be changed when the JVM initializes the variable. Use the final keyword to define constants. Just as there are two types of fields-instances and class fields, there are two constants-instance constants and class constants. To improve efficiency, you should create a class constant, or a final static field. classconstants{finalintFirst =1; finalStatic intSECOND =2; Public Static voidMain (string[] args) {intiteration =SECOND;if(Iteration = = first)//Compile ErrorSystem. out. println ("first iteration");Elseif(Iteration = =SECOND) System. out. println ("second Iteration");} The constants class in the previous example defines a pair of constants--first and second. First is an instance constant, because the JVM assigns a copy of one to each constants object. Conversely, because the JVM creates only one copy of the second after the constants class is loaded, second is a class constant. Note: A compilation error is caused when you try to access first directly in main (). The constant first exists until an object is created, so first can only be accessed by the object-not the class. 

Static/transient,final/volatile description in Java

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.