Use of final in Java and Javafinal
Concept
Final has the meaning of "unchangeable" and can modify non-abstract classes, non-abstract member methods, and variables.
- Classes modified with final cannot be inherited, and no subclass exists.
- The Methods Modified with final cannot be overwritten by the quilt class ).
- A variable modified with final indicates a constant and can only be assigned once (when a variable is declared ).
Note:
Final cannot be used to modify constructor methods, because the concept of "method override" applies only to member methods of classes, rather than constructor methods of classes, there is no overwrite relationship between the constructor of the parent class and the constructor of the Child class. Therefore, using final to modify the constructor is meaningless.
The Methods Modified by private in the parent class cannot be overwritten by the methods of the quilt class. Therefore, it can be understood that the methods of the private type are of the final type by default.
Final class
Define the class as final so that the class cannot be inherited. The specific application scenarios are as follows:
- Instead of a class designed specifically for inheritance, there is a complex call relationship between methods of the class itself. If you create any subclass of these classes, the subclass may incorrectly modify the implementation details of the parent class.
- For security reasons, the implementation details of the class cannot be extended.
- When creating an object model, make sure that this class will not be extended.
For example, the java. lang. String class in JDK is defined as the final type.
Public final class String {...}
Final Method
In some cases, for security reasons, the parent class does not allow subclass to overwrite a method. In this case, you can declare this method as the final type.
For example, in the java. lang. Object Class of JDK, The getClass () method is of the final type, while the equals () method is not of the final type.
All Object subclasses can override the equals () method, but cannot override the getClass () method.
Final variable
Variables modified with final indicate constants whose values do not change.
For example, two constants are defined in the java. lang. Integer class in JDK.
Public static final int MIN_VALUE = 0x80000000;
Public static final int MAX_VALUE = 0x7fffffff;
Final variables have the following features:
- The final modifier can modify static variables, member variables, and local variables to represent static constants, instance constants, and local constants respectively.
1 public class Demo {2 public static final int MAX_VALUE = 23; // static constant 3 public static final int MIN_VALUE = 10; // static constant 4 private final Date birthday = new Date (); // member constant 5}
Static constants are generally named after uppercase letters, separated.
- The final modified member variables must be initialized.
1 public class FinalTest {2 final int a = 1; // The default initialization of a member constant is 3 static final int B = 2; // The default initialization of a static constant is 4 final int c; // member constant 5 static final int d; // static constant 6 7 public FinalTest () {8 c = 3; // The member constant is not initialized by default, you can initialize 9} 10 11 static {12 d = 4 in the constructor; // static constants are not initialized by default. You can initialize 13} 14} in the static code block}
- The final variable can only be assigned a value once. If a variable of the reference type is modified with final, the variable can only always reference one object, but can change the content of the object.
1 public class FinalTest { 2 String str = ""; 3 public void print(){ 4 System.out.println(str); 5 } 6 public static void main(String[] args){ 7 final FinalTest finalTest = new FinalTest(); 8 finalTest.str = "xixihaha"; 9 finalTest.print();10 }11 }
Summary:
In the actual program, the final modifier is used to define constants. The purpose is:
- Improve program security.
- Improve code maintainability.
- Provides code readability.
Reference: Java Object-Oriented Programming