final

The final keyword can be used to modify variables, which are constants and cannot be modified once defined.

final double PI = 3.14;
Copy the code

Global constants are defined with public static final

public static final double PI = 3.14;
Copy the code

Constants can also be defined with the Enum type Enum

public enum MyEnum { MY_A(123), MY_B(456); private final int value; MyEnum(int value) { this.value = value; } public int getValue() { return value; } public static void main(String[] args) { System.out.println(MyEnum.MY_A.getValue()); }}Copy the code

The final keyword can be used to modify methods. Methods modified by final cannot be overridden. Methods modified by final are more efficient than non-final methods.

final void eat(a) {
	System.out.println("I am final method");
}
Copy the code

The final keyword can be used to modify classes. Classes modified by final cannot be inherited and cannot be modified.

final class Persion(a) {}Copy the code

finally

Finally is a block of exception handling that is executed regardless of whether an exception occurs in the program.

try {
	// Block of program code
} catch(Exception e) {
	// Handle exceptions
} finally {
	/ / block
}
Copy the code

finalize

Finalize is a method. If an object calls finalize, that object will be garbage collected.