The final keyword for Java is often used in daily work, such as when defining constants. If a C + + programmer is born, it may be analogous to the C + + language of the define or the Const keyword, but in fact, they are still in the semantic gap is quite large.
In Java, final can be used to decorate classes, methods, and variables (including member variables and local variables). Let's take a brief look at these few uses of the final keyword.
First, Final modified class
One common example is the string class. When a class is decorated with final, it indicates that the class cannot be inherited and that all member methods in the final class are implicitly specified as the final method, but the member variables do not change.
In general, we try not to design classes as final classes unless we have to do so for some reason. For example, you do not want the class to be inherited, which means you do not want the class to be modified. If we inherit the string class, we can define a string class that can be modified, which is almost a disaster for the consumer of the string class.
Second, final modification method
There are two reasons for using the final decoration method:
-
is to lock the method in case any inherited class modifies its meaning;
-
is efficiency. In earlier versions of Java implementations, the final method was converted to inline invocation. However, if the method is too large, you may not see any performance gains from inline calls. In the recent Java release, these optimizations do not need to be made using the final method.
Similar to the final class, the method is set to final only if you want to explicitly prohibit the method from being overwritten in subclasses. In addition, the private method of the class is implicitly specified as the final method, and its semantics require that the private method cannot be redefined.
Third, final modified variables
The place where final is most used is the modifier variable. For a final variable, if it is a variable of the base data type, its value cannot be changed once initialized, and if it is a variable of a reference type, it cannot be pointed to another object after it is initialized, but the content of the object it points to is mutable.
Because the final variable cannot be modified after initialization, the Java compiler optimizes it for this feature. When the final variable is the base data type and the string type, if you know its exact value during compilation, the compiler uses it as a compile-time constant. This optimization usually does not have much effect on the program logic, but it may be unexpected if you compare it with = =. Refer to the previous article, "Java Automatic boxing and unpacking."
In summary, final modifies the class and method, and the delegate cannot modify its definition, whereas when modifying a variable, it is similar to the const keyword in C + +, which is used to represent constants.
a good programming habit is that we should try to declare a variable as final unless the variable must be mutable. For example, when you do not need to change variables as arguments in a method, you can use final to declare them, which prevents you from unintentionally modifying them, especially if your method is very long and complex. Of course, the final modifier parameter does not affect variables outside the method.
The benefit of using the final keyword is that it ensures the security of the initialization process, unrestricted access to immutable objects, and the need to synchronize these objects in multiple threads. when we have multiple basic types of variables that they need to keep the data consistent, the usual approach is to use the Synchronized keyword to guarantee the atomicity of these variable operations. If you use the final keyword, we can define a new class that contains these variables (with the final decoration) so that the variables become constants, and the variable modification operation becomes an assignment (atomic), thus avoiding the use of lock synchronization. Of course, non-modifiable variables can also cause a lot of small object generation, aggravating the burden of garbage collection, compared to the neglect.
Finally, we know that the method parameters of the anonymous inner class can only access the final type of local variables , and the compiler also enforces this, which is why? the root cause is that the life cycle of the local variable is inconsistent with the life cycle of the anonymous inner class object. after the method of the local variable is executed, the anonymous inner class object still exists, and the anonymous inner class object cannot access the local variable because the local variable's life cycle has ended. Using the final modifier of a local variable is actually a copy of the local variable, and the copy directly acts as a data member within the local interior, thus resolving the problem of inconsistent life cycle.
Talk about the final Java keyword