final keyword in JAVA

final keyword

Final means final and unchangeable

There are four common uses:

  • 1. Can be used to modify a class
  • 2. Can be used to modify a method
  • 3. It can also be used to modify a local variable
  • 4. It can also be used to modify a member variable
final modifier class

Be careful:
1. If a class is final, it cannot have any subclasses
2. None of the member methods can be overridden because there is no subclass

//Currently, this class cannot have any subclasses. (eunuch) but with a father
public final class MyClass {

    public void method(){
        System.out.println("Method execution");
    }
}

public class MySubClass /*extends MyClass*/ {
//Error reporting, the class modified by final cannot have any subclasses
}

final decorated member method

Be careful:
1. When the final keyword is used to modify a method, the method is the final method, that is, it cannot be overridden.
2. For classes and methods, abstract keyword and final keyword cannot be used at the same time because of contradiction.

public class Fu {

    public final void method(){
        System.out.println("Parent method execution");
    }
}

public class Zi extends Fu {

    //Wrong writing cannot override the final method in the parent class
//    @Override
//    public void method() {
//        System.out.println("subclass override override override parent method");
//    }
}
final decorated local variable

Be careful:
1. Once final is used to modify a local variable, the variable cannot be changed.
2. No assignment can be made during creation, and only one assignment can be guaranteed later.
Immutable:

  • For basic types, immutability refers to the immutability of data in variables.
  • For a reference type, immutability means that the address value in the variable cannot be changed.
public class Student {
    private String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class Demo01Final {
    public static void main(String[] args) {
        int num1=10;
        System.out.println(num1);//10
        num1=20;
        System.out.println(num1);//20

        //Once final is used to decorate a local variable, the variable cannot be changed.
        //One assignment never changes
        final int num2=200;
        System.out.println(num2);
//        num2=250; / / wrong writing cannot be changed


        //The correct writing method only needs to guarantee the only assignment
        final int num3;
        num3=30;


        Student stu1=new Student("Tiger and tiger rush");
        System.out.println(stu1);
        System.out.println(stu1.getName());//Tiger and tiger rush
        stu1=new Student("Liu Wen Hu");
        System.out.println(stu1);
        System.out.println(stu1.getName());//Liu Wen Hu

        System.out.println("-----------------------------");

        final Student stu2=new Student("Monkey");
        System.out.println(stu2.getName());
        //The address in the reference type variable of error writing final cannot be changed
//        stu2=new Student("Luban");

        stu2.setName("Little Luban");
        System.out.println(stu2.getName());

    }
}

final decorated member variable

Be careful:
1.final modifies the member variable, which is also immutable.
2. Member variable has default value, so it must be assigned manually after final is used.
3. For the member variables of final, either directly or through the construction method. Choose one of them.
4. It must be ensured that all overloaded construction methods in the class will eventually assign values to the member variables of final.

public class Person {
    private final String name;

    public Person() {
        this.name="Tiger and tiger";//Make sure it's created and assigned
    }

    public Person(String name) {

        this.name = name;
    }

    public String getName() {
        return name;
    }

//    public void setName(String name) {
//        this.name = name;
//    }
}

Published 34 original articles, won praise 3, visited 948
Private letter follow

Posted on Wed, 05 Feb 2020 01:30:25 -0800 by mullz