Deep understanding of the final keyword in Java_ Power node Java college sorting

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 it mean to declare variables, methods, and classes as final? What are the benefits of using final? Finally, there are some examples of using the final keyword. Final is often used 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, 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. If you try to initialize the variable again, the compiler will report a compilation error.

What is the final variable?

Any member variable or local variable (the variable in the method or code block is called local variable) declared as final is called final variable. The final variable is often used with the static keyword as a constant. The following is an example of a final variable:

The final variable is read-only.

What is the final method?

Final can also declare methods. Method is preceded by the final keyword, which means that this method cannot be overridden 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 the method 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 run time. The following is an example of the final method:

What is the final class?

The class decorated with final is called final class. Final classes are usually functional and cannot be inherited. Many classes in Java are final, such as string, interger and other wrapper classes. The following is an example of the final class:

Benefits of the final keyword

The following summarizes some of the benefits of using the final keyword

1. The final keyword improves performance. Both the JVM and the Java application cache the final variable.

2. The final variable can be shared safely in a multithreaded environment without additional synchronization overhead.

3. Using the final keyword, the JVM will optimize methods, variables and classes.

Immutable class

To create an immutable class, use the final keyword. Immutable class means that its object cannot be changed once it is created. String is the representative of immutable class. Immutable classes have many advantages. For example, their objects are read-only, can be shared safely in a multi-threaded environment without additional synchronization overhead, and so on.

Important knowledge points about final

1. Final keyword can be used for member variables, local variables, methods and classes.

2. The final member variable must be initialized at the time of declaration or in the constructor, otherwise a compilation error will be reported.

3. You cannot assign a value to the final variable again.

4. Local variables must be assigned at the time of declaration.

5. In an anonymous class, all variables must be final variables.

6. The final method cannot be overridden.

7. Final class cannot be inherited.

8. The final keyword is different from the finally keyword, which is used for exception handling.

9. The final keyword is easily confused with the finalize () method, which is a method defined in the object class and called by the JVM before garbage collection.

10. All variables declared in the interface are final.

11. The keywords final and abstract are inversely related, so the final class cannot be abstract.

12. The final method is bound at the compilation stage, which is called static binding.

13. Those final variables that are not initialized at the time of declaration are called blank final variables. They must be initialized in the constructor or by calling this(). If you do not do so, the compiler will report an error "final variable (variable name) needs to be initialized".

14. Declaring classes, methods and variables as final can improve performance, so that the JVM has the opportunity to estimate and optimize.

15. According to the Java code convention, the final variable is a constant, and usually the constant name should be capitalized:

For a collection object, declaring final means that the reference cannot be changed, but you can add, delete or change content to it. For example:

We already know what final variables, final methods and final classes are. Use final when necessary to write faster and better code.

The above is a small series to introduce you, in-depth understanding of the final keyword in Java_ The power node is sorted out by Java college. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>