finalize

finalize

当某个类重载finalize()方法后,该类的实例对象在没被引用而被GC清理时会执行finalize()方法。

  • 文档原文:
    Called by the garbage collector on an object when garbage collectiondetermines that there are no more references to the object.A subclass overrides the finalize method to dispose ofsystem resources or to perform other cleanup.
	代码清单:
	protected void finalize()
	{
		switch(m_id)
		{
		case 1:
			System.out.print("《飘》");
			break;
		case 2:
			System.out.print("《java程序设计教程》");
			break;
		case 3:
			System.out.print("《罗马假日》");
			break;
		default:
			System.out.print("未知书籍");
			break;
	    }
		System.out.println("所对应的实例对象存储单位被收回");
	 }
	
	public static void main(String[] args)
	{
		J_book book1=new J_book(1);
		new J_book(2);
		new J_book(3);
		System.gc();// TODO Auto-generated method stub
	}

	执行结果:
	 《罗马假日》所对应的实例对象存储单位被收回
	 《java程序设计教程》所对应的实例对象存储单位被收回

你可能感兴趣的:(JavaSE)