Let's talk about the final keyword and javafinal keyword of Java.

Source: Internet
Author: User

Let's talk about the final keyword and javafinal keyword of Java.

Java final keywords are often used in daily work, such as when defining constants. If it is a C ++ programmer, it may be analogous to the define or const keywords in C ++. However, they are quite different in semantics.

In Java, final can be used to modify classes, methods, and variables (including member variables and local variables ). Let's briefly introduce the usage of final keywords.

I. final Modifier

A common example is the String class. When a class is modified with final, it indicates that the class cannot be inherited, and all member methods in the final class will be implicitly specified as final methods, but the member variables will not change.

In general, we should 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, that is, 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. This is almost a disaster for users of the String class.

Ii. final modification method

There are two reasons for using the final modifier:

  1. Lock the method to prevent any inheritance class from modifying its meaning;

  2. Is efficiency. In earlier Java implementations, the final method is converted into an embedded call. However, if the method is too large, you may not be able to see any performance improvement caused by embedded calls. In the latest Java version, the final method is not required for these optimizations.

Similar to the final class, the method is set to final only when you want to explicitly disable this method from being overwritten in the subclass. In addition, the private method of the class is implicitly specified as the final method, and its semantics requires that the private method cannot be redefined.

3. final variable Modification

The most used part of final is variable modification. For a final variable, if it is a basic data type variable, its value cannot be changed once it is initialized; if it is a reference type variable, after initialization, you cannot point it to another object, but the content of the object to which it points is variable.

The final variable cannot be modified after initialization. the Java compiler optimizes this feature. When the final variable is of the basic data type and String type, if the exact value of the final variable is known during compilation, the compiler uses it as a compile-time constant. This optimization usually does not have much impact on the program logic, but it may be unexpected if it is compared with =. For details, refer to the previous article Java automatic packing and unpacking.

To sum up, final cannot modify its definition when modifying classes and methods. In variable modification, 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 variables as final unless they must be variable.For example, if you do not need to change the variable used as a parameter in the method, you can use final to declare it. This prevents unintentional modification, especially when your method is very long and complex. Of course, whether to use final to modify parameters does not affect variables other than methods.

Another benefit of using the final keyword is that it ensures the security of the initialization process, allows unrestricted access to immutable objects, and does not need to be synchronized when multiple threads share these objects.When we have multiple basic types of variables and they need to maintain data consistency before, the common method is to use the synchronized keyword to ensure the atomicity of these variable operations. If the final keyword is used, we can define a new class that contains these variables (modified with final). In this way, these variables become constants, and variable modification operations become value assignment operations (atomic operations ), this avoids lock synchronization. Of course, unmodifiable variables will also lead to the generation of many small objects, increasing the burden of garbage collection, which can be ignored.

Finally, we know that,The method parameters of the anonymous internal class can only access local variables of the final type.The compiler also enforces this. Why?The root cause is that the lifecycle of a local variable is inconsistent with that of an anonymous internal class object.After the local variable method is executed, the anonymous internal class object still exists, and the anonymous internal class object cannot access the local variable because the lifecycle of the local variable has ended. Using final to modify local variables is actually a copy of the local variables that are directly used as data members in the Local System. This solves the issue of inconsistent lifecycles.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.