《Cracking the Coding Interview》——第14章:Java——题目3

时间:2021-07-29 23:12:26

2014-04-26 18:59

题目:final、finally、finalize有什么区别?

解法:烂大街之java语法题。此题被多少公司考过我不知道,反正我确实遇见过一次了。

代码:

 // 14.3 final, finally and finalize, what are they?
// you can't inherit me
public final class TestJava {
// you can't modify me
public final int i = 123;
public final int j; public TestJava () {
j = 2;
// you can't modify me again.
// j = 3;
} // you can't override me
public final void f() {
System.out.println("TestJava.f()");
}; // a substitute for ~TestJava().
// it signifies it is ready to be collected, but it doesn't have to be destroyed immediately.
@ Override
protected void finalize() {
System.out.println("finalized");
}; public static void main(String[] args) {
TestJava testJava = new TestJava();
testJava.f();
testJava.finalize();
}
}