The final keyword

The final keyword prevents data from being modified and ensures code security.

Can be used to modify: classes, methods in classes, variables, parameters;

decorator

The relevant code

final class Person {}// Error, cannot inherit class modified by final
class Student extends Person {}Copy the code

concept

When the final keyword is used to modify a class, it indicates that the class cannot be inherited, and all methods in the class are final by default.

Modifying a class with final modifiers prevents it from being inherited, such as the String class in JDK code that is modified with final, thus preventing it from being inherited by other classes and resulting in the internal logic being corrupted.

Modify a method in a class

code

class Person {
    // Disallows subclasses from overriding this method to change its meaning
    final String getIdentity(a) {
        return "I am a person!"; }}class Student extends Person {
    // Error, disallow overwriting this method of parent class
    @Override
    final String getIdentity(a) {
        return "I am a Student!"; }}Copy the code

concept

We can use the final modifier when we do not want a subclass to override a method of the parent class.

Then no subclass may override the method of its parent class.

Modify variables

code

public static void main(String[] args) {
    final int num;
    final Object o;
    num = 10;
    o = new Object();

    // The next two lines are wrong
    num = 20;
    o = new Object();
}
Copy the code
public class Test {
   // Final member variables ina class must be initialized in the constructor
   private final Object object;
   
   public Test(a) {
       this.object = new Object();
   }
   
   // error, a final variable must be initialized after the constructor in the class executes!!
   public Test(Object object) {}}Copy the code

concept

When we modify a variable with the final keyword.

  1. When modifying a variable of built-in type, the value of the variable cannot be modified after initialization.
  2. When a variable is of reference type, the object reference in the variable does not point to any other object after initialization, but the contents of the object can be modified.
  3. When we usefinalmodifiedMember variables, must ensure thatClass constructorAfter execution, the variable is initialized

Modifying variables with final prevents data from being modified and ensures security.

Modify parameters

code

public void testInnerType(final int num) {
    num = 10; // error: Cannot assign a value to final variable 'num';
}

public void testObject(final Object o) {
    o = new Object(); // error: Cannot assign a value to final variable 'o';
}
Copy the code

concept

It makes no sense to modify a parameter with final, even though you can do so without modifying it.

The built-in types in Java are copied as values as parameters, and the final modifier ensures that the parameters will not be modified, not that the arguments will be modified.

In Java, reference types are copied by reference as parameters, which is equivalent to adding a reference to the object that the argument points to. Final modification guarantees that the parameter does not modify the object to which it points, and does not guarantee that the argument will be modified.

Therefore, using final does not prevent the argument from being modified, so there is no need to add final to the argument. Use final if you want the parameter not to be modified by this method.