Final meaning
Final is a reserved keyword in Java that can declare member variables, methods, and classes. Once you declare the reference as final, you will no longer be able to change the reference. The compiler will check the code, and if you attempt to initialize the variable again, the compiler will report a compilation error.
Final Variable
A final variable is called final when a member variable or a local variable (a variable in a method or in a code block is called a local variable). Here is an example of the final modifier variable:
final int constvalue = 1;
Constvalue = 2; The final local variable constvalue cannot is assigned.
Add point:
- The final member variable must be initialized at the time of the declaration or initialized in the constructor, otherwise the compiler will complain.
- You cannot assign a value to the final variable again.
- All variables in an anonymous class must be final variables.
Final method
Final can also declare methods. The method name is preceded by the final keyword, which means that the method cannot be overridden by a quilt class. If you think that the function of a method is sufficiently complete, the subclass does not need to change the method, you can declare the method as final type.
The final declaration method is faster than the final method because it is statically bound at compile time and does not need to be dynamically bound at run time. The following is an example of the final method:
public class person
{public
final void Printname () {
System.out.println ("Wangzhengyi");
}
Final class
The class that is decorated with final is called the final class. Final classes are usually complete and cannot be inherited. Many of the classes in Java are final, such as the String class.
One of the benefits of immutable classes is that it is thread-safe, and you don't need to consider thread-safety issues in multithreaded environments. To create immutable classes, you need to implement the following steps:
- Declare a class as final so that it cannot be inherited.
- All members are declared private so that they are not allowed to be accessed directly.
- Do not provide setter methods for variables.
- Declare all mutable members as final so that they can only be assigned one time.
- Initializes all members through the constructor, making a deep copy (deep copy).
- In the getter method, do not directly return the object itself, but instead clone the object and return a copy of the object.
Note: The difference between a shallow copy and a deep copy:
Shallow copy (Shallow clone): All variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object. In other words, only the base type of the object is copied, and the object type still belongs to the original reference.
Deep copy (Deep clone): All variables copied contain the same values as the original objects, excluding those that refer to other objects. The variables that refer to other objects will point to the new object that was copied, not the original referenced object. In other words, not only the basic type of the object is copied, but also the objects in the original object are copied.
Examples of immutable classes:
Import Java.util.HashMap;
Import Java.util.Iterator;
public class Finalclassexample {private final int id;
Private final String name;
Private final Hashmap<integer, string> Hmap;
public int getId () {return id;
Public String GetName () {return name; /** * Access method for Mutable objects: Returns the copy of the object * @return/Public Hashmap<integer, String> Gethmap () {return (HashMap
<integer, string>) Hmap.clone (); /** * Implement deep copy of the constructor/public finalclassexample (int ID, String name, Hashmap<integer, string> map) {th
Is.id = ID;
THIS.name = name;
Hashmap<integer, string> tMap = new Hashmap<integer, string> ();
int key;
Iterator<integer> iterator = Map.keyset (). iterator ();
while (Iterator.hasnext ()) {key = Iterator.next ();
Tmap.put (Key, Map.get (key));
} this.hmap = TMap; /** * Implements the shallow copy constructor/* Public finalclassexample (int ID, string name, Hashmap<integer, string> map) {this.id = ID;
THIS.name = name;
This.hmap = map;
}
*/
}
Final keyword Benefits
The advantages of using the final keyword are as follows:
- The final keyword improves performance. Both the JVM and the Java application cache the final variable.
- Final variables can be safely shared in a multithreaded environment without the need for additional synchronization overhead.
- Using the final keyword, the JVM optimizes methods, variables, and classes.