The final keyword in Java is explained in detail using the example

  • 2020-04-01 02:44:40
  • OfStack

Final is often used with static to declare constants, and you'll see how final can improve application performance.
What does the final keyword mean?
Final is a reserved keyword in Java that allows you to declare member variables, methods, classes, and local variables. Once you declare the reference final, you cannot change the reference, the compiler checks the code, and if you try to initialize the variable again, the compiler will report a compilation error.
What is a final variable?
Anything that is declared final to a member variable or a local variable (called a local variable ina method or ina code block) is called a final variable. The final variable is often used with the static keyword as a constant. Here is an example of a final variable:


public static final String LOAN = "loan" ;
LOAN = new String( "loan" ) //invalid compilation error

Final variables are read-only.

What is the final method?
Final can also declare methods. The final keyword before the method means that the method cannot be overridden by a subclass method. You can declare a method final if you think it's fully functional enough that you don't need to change it ina subclass. Final methods are faster than non-final methods because they are statically bound at compile time and do not need to be dynamically bound at run time. Here is an example of the final method:


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 a final class?
Classes that are decorated with final are called final classes. Final classes are usually fully functional and cannot be inherited. There are many classes in Java that are final, such as String, Interger, and other wrapper classes. Here is an instance of the final class:


final class PersonalLoan{
}
class CheapPersonalLoan extends PersonalLoan{ //compilation error: cannot inherit from final class
}

Some of the benefits of using the final keyword are summarized below

The final keyword improves performance. Both the JVM and Java applications cache final variables.
Final variables can be safely Shared ina multithreaded environment without additional synchronization overhead.
With the final keyword, the JVM optimizes methods, variables, and classes.
Immutable classes
The final keyword is used to create immutable classes. An immutable class is one whose objects cannot be changed once they are created. A String is a representation of an immutable class. Immutable classes have many benefits, such as their objects being read-only, being able to share securely in a multithreaded environment, not having to incur additional synchronization overhead, and so on.
Related: why String is immutable and how to write an immutable class.
Important things about final
The final keyword can be used for member variables, local variables, methods, and classes.
Final member variables must be initialized at declaration time or in the constructor, otherwise a compilation error will occur.
You cannot assign a value to a final variable again.
Local variables must be assigned at declaration time.
All variables in an anonymous class must be final.
The final method cannot be overridden.
Final classes cannot be inherited.
The final keyword is different from the finally keyword, which is used for exception handling.
The final keyword is easily confused with the finalize() method, which is defined in the Object class and called by the JVM before garbage collection.
All variables declared in the interface are themselves final.
The keywords final and abstract are inversely related, so the final class cannot be abstract.
The final method is bound at compile time and is called a static binding.
Those that do not initialize final variables at declaration time are called blank final variables (blank final variables), and they must be initialized in the constructor or called this() initialization. Otherwise, the compiler will report an error that "final variable (variable name) needs to be initialized."
Declaring classes, methods, and variables final improves performance so that the JVM has a chance to estimate and then optimize.
By Java code convention, a final variable is a constant, and the constant name is usually capitalized:
Private final int COUNT = 10;
Declaring a collection object final means that the reference cannot be changed, but you can add, remove, or change content to it. Such as:
Private final List Loans = new ArrayList();
List. The add (" home loan "); / / valid
List. The 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.


Related articles: