Finalize() 是 Object 類的方法。在對象被垃圾回收之前調用此方法。 finalize() 方法覆蓋以處理係統資源,執行 clean-up 活動並最小化內存泄漏。
用法
protected void finalize() throws Throwable
拋出
Throwable - 此方法引發異常
例子1
public class JavafinalizeExample1 {
public static void main(String[] args)
{
JavafinalizeExample1 obj = new JavafinalizeExample1();
System.out.println(obj.hashCode());
obj = null;
// calling garbage collector
System.gc();
System.out.println("end of garbage collection");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
}
}
輸出:
2018699554 end of garbage collection finalize method called
相關用法
- Java Object hashCode()用法及代碼示例
- Java Object equals(Object obj)用法及代碼示例
- Java Object toString()用法及代碼示例
- Java Object getClass()用法及代碼示例
- Java ObjectInputStream readDouble()用法及代碼示例
- Java ObjectStreamField getOffset()用法及代碼示例
- Java ObjectStreamField compareTo()用法及代碼示例
- Java ObjectInputStream readUnsignedByte()用法及代碼示例
- Java ObjectInputStream readFields()用法及代碼示例
- Java ObjectOutputStream writeFields()用法及代碼示例
- Java ObjectInputStream readFully()用法及代碼示例
- Java ObjectStreamField getName()用法及代碼示例
- Java ObjectInputStream readInt()用法及代碼示例
- Java ObjectStreamClass forClass()用法及代碼示例
- Java ObjectInputStream read()用法及代碼示例
- Java ObjectStreamField getTypeCode()用法及代碼示例
- Java ObjectOutputStream writeBytes()用法及代碼示例
- Java ObjectInputStream readUTF()用法及代碼示例
- Java ObjectInputStream close()用法及代碼示例
- Java ObjectOutputStream useProtocolVersion()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Object finalize() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。