The use of the final keyword is unconventional. Next, I will give you a good introduction to the use of the final keyword. The final keyword, which means unchangeable. So how are the variables, methods, and classes that it modifies used in Java? Keep the word “immutable” in mind and you’ll understand how it’s used.

1. Modify variables

When final modifies a variable, an immutable variable is, as its name implies, a constant. There are two types of variables in Java: basic data type and reference data type. Decorates base data types whose values cannot be changed. For reference types, the reference address cannot be changed, but the object’s content can be changed. Such as the following code:

This is a user class with a name attribute

public class User {
    private String name;

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

    public String getName(a) {
        return name;
    }

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

    @Override
    public String toString(a) {
        return "User{" +
                "name='" + name + '\' ' +
                '} '; }}Copy the code

Next, we modify the User reference object User with the final keyword, and then point User to a new object. An error occurs, while modifying the value of name works fine.

public class UserDemoTset {
    public static void main(String[] args) {
        final User user = new User("tangke");
        //user = new User("ketang"); // Cannot assign a value to final variable 'user'
        user.setName("ketang");// It can be assigned correctly
        System.out.println(user);// The printed user name is ketang}}Copy the code

As you can see, reference types modified by final keywords cannot change their reference addresses, but they can change their internal attributes.

Two, modification method

Prior to JDK1.5, final modifications were done so that final methods would not be overridden, and if methods were small, the compiler would default to inline calls to improve efficiency. After jdk1.5, the virtual machine is automatically optimized and the final keyword is used only to prevent method overwriting. After inheriting a class, a subclass only has the right to call the method, not the right to override it. Note: Final methods cannot be overridden, but can be overridden.

The parent class has a method that uses the final modifier:

class Parent {
    private int age;

    public final int getAge(a) {
        returnage; }}class Son extends Parent {
    @Override
    public int getAge(a) {
        return super.getAge();
    }// Compile error
}

Copy the code

3. Modifier class

A final modifier class indicates that the class cannot be inherited. You can use the final keyword when you want a class not to be inherited, for example, String is a final class. A class whose methods cannot be overridden can be treated as a final modifier.

Let’s leave the final keyword here. Here is Unconventional. We will share what we have learned and what we have earned. CSDN address: blog.csdn.net/qq_42254247…