Final can modify classes, member variables, local variables, and methods.
1.final Modifier member Variable
Initialization of a 1.final member variable
For final-decorated variables, the system does not initialize to 0 by default
Fina Variable Initialization method:
- Initialized at the time of definition
- Final variables can be initialized in the initialization block and cannot be initialized in a static initialization block.
- Static final variables can be initialized in a static initialization block and cannot be initialized in an initialization block.
- FINA variables can also be initialized in constructors, but static final variables are not available.
2.final Cosmetic Method
When final is used to decorate a method, it means that the method cannot be overridden by a quilt class.
3.final Cosmetic Class
Final-decorated columns are not allowed to be inherited, and the compiler treats all its methods as final, so the final analogy to the general class has higher efficiency. Abstract columns that are defined by the keyword abstract contain abstract methods that must be implemented by inheriting from its subclass, so the same class cannot be decorated with final and abstract at the same time. In the same way, final can not be used to modify interfaces. None of the final class methods can be overridden. This does not mean that the property values of the final class are immutable. To make the final class attribute value immutable, you must add the final modifier to it.
A finally statement can only be used in a try/catch statement and is accompanied by a block of statements, indicating that the statement is always executed at all time.
Copy Code code as follows:
public class Test {
public static void Main (string[] args) {
System.out.println (Returntest ());//false
}
public static Boolean returntest () {
try{
return true;
}finally{
return false;
}
}
}
The value of the expression after the return is calculated, the value is temporarily stored, and then the expression value in finally is calculated and then stored temporarily.
This will overwrite the previously stored values. Finally, go back to the previous return, take the value out of the place where the variable was temporarily stored, and go back. That is the result.