The final keyword in Java is very important. It can be applied to classes, methods, and variables. In this article, I will show you what is the final keyword? What does variables, methods, and classes declared as final represent? What are the benefits of using final? Finally, there are some instances that use the final keyword. Final is often used together with static to declare constants. You will also see how final improves application performance.
What is the meaning of the final keyword?
Final is a reserved keyword in Java. It can declare member variables, methods, classes, and local variables. Once you make the reference declaration final, you will not be able to change this reference, and the compiler will check the Code. If you try to initialize the variable again, the compiler will report a compilation error.
What is a final variable?
Any declared final variable for member variables or local variables (variables in the method or code block are called local variables) is called final variables. Final variables are often used together with static keywords as constants. The following is an example of the final variable:
Public static final String LOAN = "loan ";
LOAN = new String ("loan") // invalid compilation error
The final variable is read-only.
What is the final method?
Final can also declare methods. The final keyword is added before the method, indicating that this method cannot be overwritten by the quilt class method. If you think that the functions of a method are complete enough and do not need to be changed in the subclass, you can declare this method as final. The final method is faster than the non-final method because it has been statically bound during compilation and does not need to be dynamically bound at runtime. The following is an example of the final method:
"personal loan" CheapPersonalLoan "cheap personal loan" ; }
What is a final class?
The class modified using final is called final class. Final classes generally have complete functions and cannot be inherited. Many classes in Java are final, such as String, Interger, and other packaging classes. The following is an example of the final class:
Final class PersonalLoan {
}
Class CheapPersonalLoan extends PersonalLoan {// compilation error: cannot inherit from final class
}
Benefits of final keywords
The following summarizes some advantages of using the final keyword.
The final keyword improves performance. Both JVM and Java applications cache final variables.
Final variables can be securely shared in a multi-threaded environment without additional synchronization overhead.
Using the final keyword, JVM will optimize methods, variables, and classes.
Immutable class
The final keyword is used to create an immutable class. An unchangeable class means that its objects cannot be changed once they are created. String represents an unchangeable class. Immutable classes have many advantages. For example, their objects are read-only and can be securely shared in a multi-threaded environment without additional synchronization overhead.
Related reading: Why is String immutable and how to write an immutable class.
Important knowledge points about final
Final keywords can be used for member variables, local variables, methods, and classes.
Final member variables must be initialized during Declaration or in the constructor; otherwise, a compilation error will be reported.
You cannot assign a value to the final variable again.
The local variable must be assigned a value during declaration.
All variables in the anonymous class must be final variables.
The final method cannot be overwritten.
The final class cannot be inherited.
The final keyword is different from the finally keyword, which is used for exception handling.
The final keyword is easy to mix with the finalize () method, which is defined in the Object class and called by JVM before garbage collection.
All the variables declared in the interface are final.
The final and abstract keywords are irrelevant, and the final classes cannot be abstract.
The final method is called static binding during compilation ).
The final variables that are not initialized at Declaration are called blank final variables (blank final variable). They must be initialized in the constructor or called this () initialization. Otherwise, the compiler reports the "final variable (variable name) needs to be initialized ".
Declaring classes, methods, and variables as final can improve performance, so that the JVM has the opportunity to estimate and then optimize.
According to Java code conventions, the final variable is a constant, and usually the constant name should be capitalized:
Private final int COUNT = 10;
If the set object is declared as final, the reference cannot be changed, but you can add, delete, or change content to it. For example:
Private final List Loans = new ArrayList ();
List. add ("home loan"); // valid
List. add ("personal loan"); // valid
Loans = new Vector (); // not valid
We already know what the final variable, final method, and final class are. Use final when necessary to write faster and better code.