Home  >  Article  >  Java  >  The difference between final, finally and finalize in java

The difference between final, finally and finalize in java

王林
王林forward
2019-11-29 14:04:462179browse

The difference between final, finally and finalize in java

The difference is as follows:

final: Modifier (keyword), used to declare attributes, methods and classes, respectively indicating that attributes are immutable, methods cannot be overridden, and classes Not inheritable.

Therefore a class cannot be declared both abstract and final. Declare variables or methods as final to ensure that they will not be changed during use. Variables declared as final must be given an initial value when declared (lazy initialization can also be used), and can only be read in subsequent references and cannot be modified. Methods declared as final can also only be used and cannot be overloaded.

Java introductory teaching video recommendation: java learning video

finally: It is part of the exception handling statement structure, indicating that it is always executed.

finalize: It is a method of the Object class. Java technology allows the use of the finalize() method to do necessary cleanup work before the garbage collector clears the object from the memory. This method can be overridden to provide garbage collection. For other resource recycling, such as closing files, etc., the JVM does not guarantee that this method will always be called.

Recommended related articles and tutorials: Introduction to java language

The above is the detailed content of The difference between final, finally and finalize in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete