Summary of the final keyword usage in Java _java

Source: Internet
Author: User
Tags modifier

1. Final Cosmetic class
the final modified class cannot be inherited, so the member method of the final class cannot be overwritten, and the class decorated with the final keyword has no subclasses, so the implementation details of the class cannot be changed and cannot be extended. All member methods in the final class are implicitly specified as final methods, and the member variables in the final class can be set as final as required.

2. Final Modification method
if a method in a class is decorated by the final keyword, its subclasses cannot override the method, and can only inherit from the quilt class. If a method in the parent class does not want to be overridden by its subclass, the method can be defined as the final type, and the private method in the parent class (that is, the method modified by the Private keyword) does not allow the quilt class to overwrite, so the method decorated by the Private keyword in the parent class defaults to the final type. The method modified by the final keyword has the following characteristics:

The method is locked to prevent any inheriting class from modifying its meaning and implementation;
Efficient. When the compiler encounters the final method, it goes into the embedded mechanism, which greatly improves the execution efficiency.
In addition, a static method that uses final adornments cannot be hidden in subclasses of its owning class (hide);

3, final modifier variable
final this keyword means "This is immutable" or "final state";
so why stop the change? The creator of the
Java language may be prevented from changing for two purposes:
A. Efficiency issues:
Some methods of some classes in the JDK are not allowed to be overridden by the user, and the designer may think that the method is already the best method,
  users are covered privately, or due to negligence, it can affect the JVM or the system's energy;
B. Design requirements:
It is well known that some situations must use the final keyword, such as parameter passing of an anonymous inner class in the method.
It is said that the final variable is immutable after it is assigned,
So what does this constant mean?
This invariant refers to the reference, is the address, and the contents of the referenced object are still mutable.
that is, the final variable always points to an object, a constant pointer, not a pointer to a constant.
(1) The base data type modified by the final keyword, the value of which cannot be changed once it is initialized;
(2) If it is a variable of a reference type, it cannot be directed to another object after it is initialized, but the contents of the object to which the reference is directed can change. The reason: The address of the referencing object stored in the heap memory of the reference data type, and after the final modifier reference type, the heap memory space that the reference is required to point to (or the heap memory address of that reference store) cannot be changed.
When you modify a non-static member variable of a class with final, there are two ways to initialize a member variable:
(3) initializing at Declaration
(4) can be declared without initialization when declaring a variable, which is called blank final, or final blank, But you want to assign an initial value to this variable in all of the constructor methods of the class in which the variable is located.
When a static member variable of a class is decorated with final, there are also two ways to initialize a static member variable:
(5) initializing at Declaration
(6) initializing in a static initialization block
when the static variable of the final modifier interface is initialized with one of the following methods:
(7) initializing at Declaration
when you modify a local member variable with final, you only need to ensure that the assignment is initialized before you use it.
in short, a variable modified by the final keyword can be declared without initialization, but it must be guaranteed that the variable is initialized before it is used, and once the assignment is initialized, it cannot be assigned again. Try to analyze the output of the program:

public class Test {public
 static void Main (string[] args) {
   String a = "Hello2"; 
   Final String b = "Hello";
   String d = "Hello";
   String C = b + 2; 
   String E = d + 2;
   System.out.println ((A = = c));
   System.out.println ((A = = e));
 }

4. Final modification parameters
when a method parameter is a final type, you can read it using this parameter, but you cannot change the value of the parameter. The variable is created at the time of the method call, and is initialized to the corresponding argument value, and the value cannot be changed until the body of the method has finished executing. A parameter of the base data type decorated with the final keyword whose parameter value cannot be changed, a reference data type decorated with the final keyword, cannot be the reference to another object, or assigned to null again, but can be used to alter the contents of the referenced object. Final the purpose of modifying a parameter is not to prevent the operation of the parameter from changing the value of the external corresponding variable of the method within the method being invoked, but to prevent the parameter from being assignable in the method, affecting the initial value when the parameter is passed. Also, modifying the parameters in the method with the final keyword does not prevent the contents of the values of the parameters passed outside the method from being changed. You can refer to the final modifier reference data type interpretation.

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.