Final is often used with static to declare constants, and you will also see how final improves application performance.
What is the meaning of final keyword?
Final is a reserved keyword in Java, which can declare member variables, methods, classes, and local variables. Once you declare the reference as final, you will not be able to change the reference. The compiler will check the code and if you try to initialize the variable again, the compiler will report a compile error.
What are final variables?
Any member variable or local variable (variables in methods or code blocks are called local variables) that are declared as final is called final variable. Final variables are often used together with static keywords as constants. Here is an example of final variable:
The code copy is as follows:
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, which means that this method cannot be rewritten by subclass methods. If you think that the function of a method is complete enough and does not need to be changed in the subclass, you can declare this method final. The final method is faster than the non-final method, because it is already statically bound during compilation and does not need to be dynamically bound at runtime. Here is an example of the final method:
The code copy is as follows:
class PersonalLoan{
public final String getName(){
return "personal loan" ;
}
}
class CheapPersonalLoan extends PersonalLoan{
@Override
public final String getName(){
return "cheap personal loan" ; //compilation error: overridden method is final
}
}
What is the final class?
A class that uses final to modify is called final class. Final classes are usually functionally complete and they cannot be inherited. There are many classes in Java that are final, such as String, Interger and other wrapper classes. Here is an example of the final class:
The code copy is as follows:
final class PersonalLoan{
}
class CheapPersonalLoan extends PersonalLoan{ //compilation error: cannot inherit from final class
}
Here are some of the benefits of using final keywords
Final keyword improves performance. Both JVM and Java applications cache final variables.
Final variables can be safely shared in multi-threaded environments without the need for additional synchronization overhead.
Using the final keyword, the JVM will optimize methods, variables and classes.
Immutable Class
To create an immutable class, use the final keyword. An immutable class means that its objects cannot be changed once they are created. String is a representative of immutable classes. Immutable classes have many benefits, such as their objects are read-only and can be safely shared in a multi-threaded environment without additional synchronization overhead, etc.
Related Reading: Why String is immutable and how to write an immutable class.
Important knowledge about final
The final keyword can be used for member variables, local variables, methods, and classes.
Final member variables must be initialized at the time of declaration or in the constructor, otherwise a compilation error will be reported.
You cannot assign a final variable again.
Local variables must be assigned values at declaration time.
All variables in anonymous class must be final variables.
The final method cannot be rewritten.
The final class cannot be inherited.
The final keyword is different from the final keyword, which is used for exception handling.
The final keyword is easily confused with the finalize() method, which is a method defined in the Object class and is a method called by the JVM before garbage collection.
All variables declared in the interface are themselves final.
The two keywords final and abstract are inversely related, so the final class cannot be abstract.
The final method is bound at the compilation stage, which is called static binding.
A blank final variable that does not initialize the final variable at declaration is called a blank final variable. They must be initialized in the constructor, or this() is called initialized. If you don't do this, the compiler will report an error "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 convention, final variables are constants, and the constant names are usually capitalized:
private final int COUNT = 10 ;
Declaring final for collection objects means that the reference cannot be changed, but you can add, delete or change the 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 final variables, final methods and final classes are. Use final when necessary to write faster and better code.