final

1. final modified class

  final class Car {  //Indicates that this class cannot be inherited

  }

2. final modification method

  final void setColor() { //Indicates that this method cannot be overridden by subclasses

  }

/* If the access modifier of the method is private at this time, the same method as the parent class can be defined in the subclass. At this time, there is no conflict between final and overriding, but the subclass defines a new method by itself*/

3. Final modified variable (indicating that the value of the variable cannot be changed once it is initialized)

  final double MAX_VALUE = 3.14;

(1) When final modifies a local variable, the initialization may not be displayed, but it must be initialized before it can be used

(2) When final modifies member variables, initialization must be displayed. There are two initialization methods. One is to initialize the variable when it is declared, and the other is to not assign an initial value when the variable is declared, but assign the initial value to the variable in all constructors of the class where the variable is located.

(3) When final modifies static variables, initialization must be displayed at the time of declaration

(4) When final modifies a variable of a reference type, it cannot be allowed to point to other objects after it is initialized, but the content pointed to by the reference can change

(5) final modifiable formal parameters

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325114851&siteId=291194637