The difference between final, fianlly and finalize the


1. final keyword, final class can be modified, methods, properties. If a class is final modification, then this class is final class can not derive a new subclass, can not be a father of succession, all the methods in this class is not to be rewritten, but the final class member variables can be change, can not change the member variables in order to final class, you must add the final modifications to the member variable. Thus, a class can not be final and abstract modifications, these two keywords contradict each other. If the final modification methods, this method is the final method, it does not allow any subclasses override this method, but subclasses can still use this method, note: final parameter is used to denote that the parameter can not be modified in this internal function. attribute final modification, the modified final variables immutable. Immutable here has two meanings: Do not change references objects and immutable. final quote refers not changed, that is, it can only point to the target point at initialization, without regard to an object content changes. Therefore, the final modified variables must be initialized, the variable is in fact constant.

2. finally as part of the exception process, can only be used in a try / catch statement in fast, the finally block the statement will be executed, it is often used to release resources such as IO streams and release the database resources.

3. Object finalize is a class method, the method declaration Object class: protected void finalize () throws Throwable {} When performing the garbage collector will be subject to collection call finalize () method, can override this method to achieve recovery of its resources. Note: Once the garbage collector ready to release the space occupied by an object, the object will first tune finalize () method, and when the next garbage collection action occurs, really the object occupies memory recovery.

 

Links: https://www.nowcoder.com/questionTerminal/cfd7abfedb314172a0b3bb220d98b861
Source: Cattle-off network 

Guess you like

Origin blog.csdn.net/weixin_44961794/article/details/90693610