[读书笔记] Think in Java r_8

P108 finalize()

The odd thing about this is that you call gc( ) before you call runFinalization( ), which seems to contradict the JavaSoft documentation which claims that finalizersare run first, and then the storage is released. However, if you call runFinalization( ) first, and then gc( ), the finalizers will not be executed.

P131 protected

同一个包内的所有类可以访问,异包的子类也可以访问

P183 static inner class

To understand the meaning of static when applied to inner classes, you must keep in mind the idea that the object of the inner class implicitly keeps a handle to the object of the enclosing class that created it. When you say an inner class is static, this is not true, which means:

  1. You don’t need an outer-class object in order to create an object of a static inner class.
  2. You can’t access an outer-class object from an object of a static inner class.

P183 inheriting from inner classes

Because the inner class constructor must attach to a handle of the enclosing class object, things are slightly complicated when you inherit from an inner class. The problem is that the “secret” handle to the enclosing class object must be initialized, and yet in the derived class there’s no longer a default
object to attach to. The answer is to use a syntax provided to make the association explicit:

//: InheritInner.java
// Inheriting an inner class
class WithInner {
class Inner {}
}
public class InheritInner
extends WithInner.Inner {
//! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String args[]) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
} ///:~

You can see that InheritInner is only extending the inner class, not the outer one. But when it comes time to create a constructor, the default one is no good and you can’t just pass a handle to an enclosing object. In addition, you must use the syntax

enclosingClassHandle.super();

inside the constructor. This provides the necessary handle and the program will then compile.

P240 try,finally,Exception

//: LostMessage.java
// How an exception can be lost
class VeryImportantException extends Exception {
    public String toString() {
        return "A very important exception!";
    }
}
class HoHumException extends Exception {
    public String toString() {
        return "A trivial exception";
    }
}
public class LostMessage {
    void f() throws VeryImportantException {
        throw new VeryImportantException();
    }
    void dispose() throws HoHumException {
        throw new HoHumException();
    }
public static void main(String args[]) throws Exception {
    LostMessage lm = new LostMessage();
    try {
        lm.f();
     } finally {
        lm.dispose();
     }
    }
} ///:~

The output is:

A trivial exception
at LostMessage.dispose(LostMessage.java:21)
at LostMessage.main(LostMessage.java:29)

You can see that there’s no evidence of the VeryImportantException, which is simply replaced by the
HoHumException in the finally clause. This is a rather serious pitfall, since it means an exception can be completely lost, and in a far more subtle and difficult-to-detect fashion than the example above. In contrast, C++ treats the situation where a second exception is thrown before the first one is handled as a dire programming error. Perhaps a future version of Java will repair the problem (the above results were produced with Java 1.1).

P243 Exception Caught order

When an exception is thrown, the exception-handling system looks through the “nearest” handlers in
the order they are written.
If you try to “mask” the derived-class exceptions by putting the base-class catch clause first, like this:

try {
    throw new Sneeze();
} catch(Annoyance e) {
    System.out.println("Caught Annoyance");
} catch(Sneeze d) {
    System.out.println("Caught Sneeze");
}

The compiler will give you an error message, since it sees that the Sneeze catch-clause can never be reached.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,083评论 0 23
  • 文/连山蒂 —1— 在家乡,他是个名人。 从小到大,朋友们给他的评价出奇的一致。 ——笨,笨到家了。一首绝句他磕磕...
    连山蒂阅读 7,373评论 46 109
  • 《如果站在其他人的角度,你会对待他们不一样吗?》 回顾:第三天分身术训练营的结束时间,也是总结的一天,...
    亚里士多缺德阅读 311评论 0 0
  • 本文参加#乡忆-乡思-乡情乡恋#活动,本人承诺,文章内容为原创,且未在其他平台发表过。 (文章内容真实,略...
    叮当猫_b129阅读 460评论 0 0
  • 插入排序 插入排序是构建有序序列,然后把无序的插入有序相应位置形成新的有序序列,周而复始,全部有序.下面就用代码给...
    李某lkb阅读 148评论 0 0