The final keyword in Java is important, and it can be applied to classes, methods, and variables. In this article I will show you what is the final keyword? What does it mean to declare variables, methods, and classes as final? What are the benefits of using final? Finally, there are instances that use the final keyword. Final is often used with static to declare constants, and you'll see how final improves application performance.
What does the final keyword mean?
Final is a reserved keyword in Java and can declare member variables, methods, classes, and local variables. Once you have the reference declaration as final, you will not 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.
What is a final variable?
A final variable is always declared as final for a member variable or a local variable (in a method or a variable in a code block, called a local variable). The final variable is often used with the static keyword as a constant. The following is an example of a final variable:
Public Static " Loan " New String ("loan"//Invalid compilation error
The inal variable is read-only.
What is the final method?
Final can also declare methods. The method is preceded by the final keyword, which means that the method cannot be overridden by a quilt class. If you think that the functionality of a method is complete enough, and you don't need to change it in a subclass, you can declare that this method is final. The final method is faster than the non-final method because it is statically bound at compile time and does not need to be dynamically bound at run time. Here is an example of the final method:
1 classpersonalloan{2 Publicfinal String GetName () {3 return "Personal Loan";4 }5 }6 7 classCheappersonalloan extends personalloan{8 @Override9 Publicfinal String GetName () {Ten return "Cheap personal Loan";//compilation Error:overridden method is final One } A}
what is the final class?
A class that uses final to decorate is called the final class. The final class is generally functionally complete and cannot be inherited. There are many classes in Java that are final, such as String, Interger, and other wrapper classes. The following is an instance of the final class :
1 class personalloan{2 3 }4 5 class Cheappersonalloan extends personalloan{ //compilation Error:cannot inherit from final class6 7 }
the benefits of the final keyword
The following summarizes some of the benefits of using the final keyword
- The final keyword improves performance. Both the JVM and the Java application cache the final variable.
- The final variable can be shared securely in a multithreaded environment without the need for additional synchronization overhead.
- With the final keyword, the JVM optimizes methods, variables, and classes.
Immutable Classes
To create an immutable class, use the final keyword. An immutable class means that its objects cannot be changed once they have been created. String is the representative of the immutable class. Immutable classes have many benefits, such as their objects are read-only, can be shared securely in multithreaded environments, without additional synchronization overhead, and so on.
important points of knowledge about final
- The final keyword can be used for member variables, local variables, methods, and classes.
- The final member variable must be initialized at the time of declaration or initialized in the constructor, or a compilation error will be reported.
- You cannot assign a value to the final variable again.
- A local variable must be assigned a value at the time of declaration.
- All variables in an anonymous class must be final variables.
- The final method cannot be overridden.
- The final class cannot be inherited.
- The final keyword differs from the finally keyword, which is used for exception handling.
- The final keyword is easy to confuse with the Finalize () method, which is the method defined in the object class, which is called by the JVM before garbage collection.
- All the variables declared in the interface are final.
- The final and abstract two keywords are anti-related, and the final class cannot be abstract.
- The final method is bound during the compile phase, called static binding.
- No final variables, called blank final variable, are initialized at the time of declaration, they must be initialized in the constructor or called this () initialization. If you do not, the compiler will say "final variable (variable name) needs to be initialized".
- Declaring classes, methods, and variables as final can improve performance, so that the JVM has an opportunity to estimate and then optimize.
- According to the Java Code convention, the final variable is constant, and the pass is often capitalized:
1 |
private final int COUNT = 10 ; |
- For a collection object to be declared final refers to a reference that cannot be changed, but you can add, delete, or change the content to it. Such as:
1 2 3 4 |
private Code class= "Java keyword" >final list Loans = new arraylist (); list.add (" Home loan "); Code class= "Java comments" >//valid list.add ( ); //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, better code.
Summary of the FINALLY keyword for Java learning