(1) final: modifier (keyword). If a class is declared final, it means that it cannot derive new subclasses and cannot be inherited as a parent class. Therefore, a class cannot be declared both abstract and final. Declaring variables or methods as final can ensure that they are not changed during use. Variables declared as final must have an initial value when declared, and can only be read in future references and cannot be modified. Methods declared as final can also be used only and cannot be overloaded.
(2) finally: Provides finally blocks to perform any clearing operation during exception handling. If an exception is thrown, the matching catch clause will be executed, and the control will enter the finally block (if any).
(3) finalize: method name. Java technology allows the use of the finalize() method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector when determining that the object is not referenced. It is defined in the Object class, so all classes inherit it. The subclass overrides the finalize() method to organize system resources or perform other cleaning work. The finalize() method is called on the object before the garbage collector deletes it.
The above may be a little vague, let's discuss it more in-depth
1. Different properties
(1) Final is the keyword;
(2) Finalize() is a method;
(3) Finally is a block flag, used in try statements;
Second, function
(1) Final is a keyword used to identify constants, and the keywords identified by final are stored in the constant pool (the specific usage of final constants will be introduced below);
(2) The finalize() method is defined in Object. It is used to call the JVM to garbage collect the object when the object "disappears", similar to the destructor in C++; when user-defined, it is used to release the resources occupied by the object (such as performing I/0 operations);
(3) Finally{} is used to identify the code block and cooperate with try{}. Regardless of whether the code in the try has been executed or not (here refers to an exception), the program in the code block will definitely be carried out;
3. Detailed explanation
1 Define variables
1.1 When final defines a basic type variable, variable initialization must be required to be used in other places when declaring or in the constructor. The constants defined by this keyword cannot be changed except for the initialization stage.
1.2 Final defines a reference to an object, and the initialization of the reference is consistent with the requirements when defining a constant; the content of the object defined by the keyword can be changed, but the address pointed to by the reference cannot be changed;
2 Define parameters
If the variable defined by this parameter is passed into, the method cannot modify the content of the parameter (error), which is the same as the modification rules for defining the variable; the java method passes the value when passing the basic type in the java method, and the java method passes the parameter for the object; <After all, the method passes the method in java depends on passing a "copy": for the basic type, first create a Copy, assign the passed value to Copy, and then operate on Copy; for the object type, first create a reference Copy, and assign the passed object reference to Copy>
For example: method (final int test);
Some books say that final definition parameters here, especially the parameters of the object, are very useful and cannot change the content of the object within the method. This statement is wrong! It turns out that I also think that there are some functional programming characteristics in this way. You cannot modify the content of the object. Here you can still modify the content of the object.
? ? What's the use of defining this parameter? ?
String is born to be final!
3 Definition method
(1) Methods defined using final keyword cannot be inherited by subclasses;
(2) Allow the compiler to convert all calls to this method into inline (in-line) behavior, that is, you can copy this method directly at the call instead of making routine method calls (save breakpoints, press the stack), which will increase the efficiency of the program. However, if there are too many, this will cause code bloating and will affect efficiency, so this method should be used with caution. .
4 Definition Class
Any final class cannot be inherited by anyone, which means that this class is a leaf class in an inheritance tree, and this class is considered perfect and does not require any modification (in short, it is not recommended)