English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java final keyword in detail:
Preface:
The final keyword in Java is very important and can be applied to classes, methods, and variables. In this article, I will take you through what the final keyword is, what it means to declare variables, methods, and classes as final, what are the benefits of using final, and finally, some examples of using the final keyword. Final is often used with static to declare constants, and you will also see how final improves application performance.
The meaning of the final keyword?
The final keyword in Java is a reserved keyword that can be used to declare member variables, methods, classes, and local variables. Once you declare a reference as final, you cannot change this reference anymore; the compiler will check the code, and if you try to reinitialize the variable, the compiler will report a compilation error.
What is a final variable?
All member variables or local variables (variables within methods or code blocks are called local variables) declared as final are called final variables. Final variables are often used with the static keyword as constants. 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 a final method?63;
Final can also be used to declare methods. Adding the final keyword to a method indicates that this method cannot be overridden by a subclass method. If you think a method's functionality is complete enough and does not need to be changed in the subclass, you can declare this method as final. 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 runtime. Here is an example of a 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?
A class that is decorated with final is called a final class. Final classes are usually complete in functionality and cannot be inherited. Java has many final classes, such as String, Interger, and other wrapper classes. Here is an example of a final class:
final class PersonalLoan{ } class CheapPersonalLoan extends PersonalLoan{ //compilation error: cannot inherit from final class }
Benefits of the final keyword
Below are some of the benefits of using the final keyword
Immutable class
To create an immutable class, you must use the final keyword. An immutable class refers to an object that, once created, cannot be changed. String is an example of an immutable class. Immutable classes have many benefits, such as their objects being read-only, safe to share in a multithreaded environment, and without additional synchronization overhead, etc.
Important points about final
private final int COUNT = 10;
Declaring a collection object as final means that the reference cannot be changed, but you can add, delete, or change the content within 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. Using final when necessary can write faster and better code.
Thank you for reading, I hope it can help everyone, thank you for your support to this site!