final modifier for Java Basics

Catalog

@author: Tobin
The level is limited. If there are any mistakes, please correct them.
Refer to "Java Core Technology Volume - Basic Knowledge 10th Edition"
https://blog.csdn.net/sinat_38899160/article/details/72650686

This section discusses the use of final modifiers.
Final, as its name implies, has the final meaning. It explains the meaning of final through several questions and answers.

Q1:final modifies the basic type domain

Review the basic types.

A: An instance domain modified by final must be initialized when building an object. That is to say, it must be ensured that the value of the domain is set after each constructor is executed, and that it cannot be modified in subsequent operations. The E variable in the following code is the final modified instance that must be initialized.
In addition to instance types, final is also used to modify local and static variables. For final modification of the static variable, put it in the static to explain.
Place it in static for details.
When modifying static variables, that is static variables, such variables are also called static constants. Static constants belong to classes rather than objects. They can be accessed directly by class names. They are shared by all objects. That is, 1000 objects can have 1000 S copies (strings in the code below), but only one C, even if no object is declared. It also exists. For example, Math.PI in the Math class.

package test;

public class Employee { 
    private final String S="final Instance variables S"; 
    private final int A=100; 
    public final int B=90; 
    public static final int C=80; 
    private static final int D=70; 
    public final int E; //final blank, which must be initialized when the object is initialized 
    public Employee(int x){ 
    E=x; 
    } 
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
        Employee t=new Employee(2); 
        //t.A=101; // Error, the value of final variable cannot be changed once given 
        //t.B=91; // Error, the value of final variable cannot be changed once given 
        //t.C=81; // Error, the value of final variable cannot be changed once given 
        //t.D=71; // Error, the value of final variable cannot be changed once given 
        System.out.println(t.A); 
        System.out.println(t.B); 
        System.out.println(t.C); //Object-based access to static fields is not recommended 
        System.out.println(t.D); //Object-based access to static fields is not recommended 
        System.out.println(Employee.C); 
        System.out.println(Employee.D); 
        //System.out.println(Employee.E); // Error, because E is final blank, depending on different object values. 
        System.out.println(t.E); 
        Employee t1=new Employee(3); 
        System.out.println(t1.E); //The final blank variable E varies according to the object. 
        } 
        private void test(){ 
        System.out.println(new Employee(1).A); 
        System.out.println(Employee.C); 
        System.out.println(Employee.D); 
        } 
        public void test2(){ 
        final int a; //final blank, assigning values when needed 
        final int b=4; //Local Constant--final for Local Variables 
        final int c; //The final blank has not been assigned. 
        a=3; 
        //a=4; error, overvalued. 
        //b=2; Error, overvalued. 
        } 
}

When constructing an Employee object, it needs to be initialized. If you don't write a constructor, the compiler will report errors directly, and if you don't pass in parameters when you build an object, you will report errors as well.

Modification of Q2:final

A: The method modified by final cannot be inherited. As follows, when we want to rewrite the f2 function, we will report an error. One advantage of final modification is that it can improve the efficiency of operation. When the compiler encounters final method, it will be transferred to embedded mechanism (recommended blog https://blog.csdn.net/Java_I_ove/article/details/76946598). How to improve it is discussed in detail. We will study JVM later.

// Test1.java
package test;
public class Test1 {
    public void f1()
    {
        System.out.println("this is f1 from Test1");
    }
    
    public final void f2()
    {
        System.out.println("this is f2 from Test1");
    }
}

// Test2.java
package test;

public class Test2 extends Test1{

    public void f1()
    {
        System.out.println("this is f1 from Test1");
    }
    
    public void f2() //Errors will be reported here.
    {
        System.out.println("this is f2 from Test1");
    }

}

We naturally associate that constructors in Java can be finalized? With the final modifier before the constructor, the compiler will report an error directly. The conclusion is not possible. First of all, the constructor of the parent class is not a member function and can not be inherited, but can be called by the subclass, since it can not be inherited, it can not be rewritten. So final modifier constructors don't make any sense. As for how constructors work in inheritance, leave it to other chapters (recommended blog https://blog.csdn.net/yuxiaohui78/article/details/43887373, https://blog.csdn.net/qiumengchen12/article/details/44829907).

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
//Source: https://docs.oracle.com/javase/tutorial/java/I and I/subclasses.html

Q3:final modifier class

The final modified class is not inheritable. For example, common string classes. When a class is modified with final, methods within the class can not be overridden without final.

Q4:final modification parameter

When final modifies the parameters of the incoming method, the parameters are read-only and cannot be modified.

Q5:final modifier object

When final modifies an object, it cannot point to another object, but the content of the object can be modified.

        final Employee a = new Employee(2);
        Employee b = new Employee(3);
        a.temp = 2;//It can be executed. The final object is not allowed to change, but the content of the object can be modified.
        b = a;//Executable, b does not declare final
        a = b;//Error, final pointed object is not allowed to be replaced

Keywords: PHP Java jvm Oracle

Added by sastro on Mon, 24 Jun 2019 23:25:35 +0300