The role of the Static,final,transient,volatile keyword in Java

Source: Internet
Author: User

Static and final

Static modifier keywords, can modify variables, program block, class method;

When you define a static variable, the JVM allocates it to the memory heap, and all references to it will point to that address without reallocating the memory;

when modifying a program block (that is, writing the code directly in the static{...} , the virtual machine loads the code in the static block first, which is used primarily for system initialization;
When you modify a class method, you can call it directly from the class without needing to create a new object.

final can only be assigned one time, modifying variables, methods, and classes,
When you define a final variable, the JVM assigns it to a constant pool, and the program cannot change its value; When you define a method, the method cannot be overridden in a subclass, and when you decorate a class, the class cannot be inherited.

static and final use scopes: classes, methods, variables.

2. Distinction and Contact:

2.1.static Meaning: Static, statically decorated methods and properties belong to any object that the class does not belong to the class.
2.2.static Usage:
2.2.1.static can decorate "inner classes", methods, and member variables.
2.2.2.static "Cannot decorate the outer class", "cannot decorate the local variable" (because static is defined as the class level, so the local level variable is not static decorated).

2.3 Final Meaning: "Can only be assigned once".
2.4.final Usage:
a 2.4.1.final decorated property that indicates that the property can only be assigned once, (1) The base type: The value cannot be modified, and (2) the reference type: The reference cannot be modified.
the 2.4.2.final decoration method, which indicates that the method cannot be overridden, but can be accessed by a quilt class if the method is not private type.
a 2.4.3.final decorated class that indicates that a class cannot be inherited.

3. Combined use of static final

3.1. Scope of application:

3.1.1. The intersection of the two ranges, so it can only be modified: member variables, methods, inner classes.

3.2. Meaning: Also the intersection of the two:

3.2.1. Method: A method that belongs to a class and cannot be overridden.
3.2.2. Member variable: A variable that belongs to a class and can be assigned only once.
3.2.3. Inner class: Belongs to an external class and cannot be inherited


Type modifiers, which can only be used to decorate a field, if an instance variable is declared with transient, its value does not need to be maintained when the object is stored. In other words, a member variable tagged with the transient keyword does not participate in the serialization process.

Volatile
Volatile is also a variable modifier and can only be used to modify variables. A volatile-modified member variable forces the value of the member variable to be reread from shared memory each time it is accessed by the thread. Also, when a member variable changes, forcing the thread to write the change back to the shared memory. So at any moment, two different threads always see the same value for a member variable.

Explain Java's memory mechanism here:

Java uses one main memory to hold the current value of the variable, and each thread has its own working memory. When a thread accesses a variable, it copies the value of the variable into its own working memory, so that when the thread operates on a variable in its own working memory, the value of the variable copied in the working memory is different from the value of the variable in the main memory.

The Java language specification states that for optimal speed, a thread is allowed to save a private copy of a shared member variable, and is compared to the original value of a shared member variable only if the thread enters or leaves the synchronized code block.

This way, when multiple threads interact with an object at the same time, it is important to notice that the thread gets the changes to the shared member variables in a timely manner.

The volatile keyword is a hint to the VM: You cannot save its private copy for this member variable, but you should interact directly with the shared member variable.

Usage Recommendation: Use volatile on member variables accessed by two or more threads. You do not have to use the variable you want to access when it is already in a synchronized code block, or is a constant.

It is inefficient to use volatile to mask the necessary code optimizations in the VM, so use this keyword when necessary.

The role of the Static,final,transient,volatile keyword 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.