Inheritance disadvantage Unlike method invocation, inheritance violates encapsulation. Since you don't know the super class implementation which may involve some unpredictable calling between different methods of super class. And it is difficult to m…
Use generic types to replace the object declaration Add one or more type parameters to its declaration. Replace all the uses of the type Object with the appropriate type parameter. Handle the new E[] errorwith two ways : Use the explicit type casting…
Static utility methods are particularly good candidates for generification. The type parameter list, which declares the type parameter, goes between the method's modifiers and its return type. // Generic method public static <E> Set<E> union(S…
Benefits to reuse preexisting exceptions It makes your API easier to learn and use. Programs using your API are easier to read. Fewer exception classes mean a smaller memory footprint and less time spent loading classes. Common Exceptions Exception O…
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st language I have chosen for this migration. It's a nice chance to read some great books like "Effective Java 2nd Edition" and share the note for what I…
Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计会封装所有实现细节,模块之间只能通过他们公开的API进行交流.因为这些模块互不影响(loosely coupled),所以可以进行单独测试等等.所以为了封装,我们应该把每一个class或member弄得越inaccessible越好.顶层的class和interface只能是public或者defa…
MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性等等,反正就是很有必要和有用. 四个修饰等级  private :只能当前class中调用 package-praivate 声明class,没有修饰的时候是默认 package-praivate,表示在一个 package里面使用 protect  subclass里面调用 public 任意地方…
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating and Destroying Objects Consider static factory methods instead of constructors Consider a builder when faced with many constructor parameters Enforce the s…
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Objects Consider static factory methods instead of constructors (factory方法可以拥有名称,可以避免重复创建,比如单例模式) Consider a builder when faced with many constructor parameter…
Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几种情况不需要你override这个类的equals方法:这个类的每一个实例天生就是unique的,比如Thread:这个类的父类已经override了equals,并已经足够:这是个private或者package的class,你确定equals永远不会被调用,保险起见这时候你其实可以overrid…