Advantage Unlike constructors, they have names. (BigInteger.probablePrime vs BigInteger(int, int, Random) They are not required to create a new object each time they're invoked(Flyweight pattern). This ensures that a.equals(b) if and only if a = = b.…
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…
<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…
第1章 引言 第2章 创建和销毁对象 第1条:考虑用静态工厂方法代替构造器(Consider static factory methods instead of constructors) 第2条:遇到多个构造器参数时要考虑用构建器(Consider a builder when faced with many constructor parameters ) 第3条:用私有构造器或者枚举类型强化Singleton属性( Enforce the singleton property with a…
本文参考 本篇文章参考自<Effective Java>第三版第六条"Avoid creating unnecessary objects" avoid creating unnecessary objects by using static factory methods in preference to constructors on immutable classes that provide both 若一个类同时提供了静态工厂方法和构造方法,尽量使用静态工厂方法,…
Chapter 4 Classes and Interfaces Item 13: Minimize the accessibility of classes and members 一个好的模块设计会封装所有实现细节,模块之间只能通过他们公开的API进行交流.因为这些模块互不影响(loosely coupled),所以可以进行单独测试等等.所以为了封装,我们应该把每一个class或member弄得越inaccessible越好.顶层的class和interface只能是public或者defa…
Nested class types Usage and remark Advantage Disadvantage static member classes Use for public helper class, useful only in conjunction with its outer class. (Such as the operation types of a Calculater). Save memory. Class specific. Not instance sp…
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…
Java Native Interface(JNI) allows Java applications to call native methods, which are special methods written in native programming languages such as C or C++. Historical usages of native methods Access to platform-specific facilities such as registr…
本文参考 本篇文章参考自<Effective Java>第三版第二条"Consider a builder when faced with many constructor parameters" 静态工厂方法和构造器的不足之处 当一个类中有大量的字段时,尽管能通过设置不同的形参列表和方法名进行重载或使用静态工厂方法进行一定的简化,但我们还是会难以避免地为方法签名设置了较长的形参列表并直接使用到这些方法,以致于"it is hard to write client…
<Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一个字一个字去推敲,研究.所以,书呆子们一般都很xx,在我眼里. 2016.07.24作 1,用静态方法替代构造器,下面是很好的例子: public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE…