`
leonzhx
  • 浏览: 763168 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 4: Enforce noninstantiability with a private constructor

阅读更多

1.    Classes that are just a grouping of static methods and static fields can be used to group related methods on primitive values or arrays, in the manner of java.lang.Math or java.util.Arrays. They can also be used to group static methods, including factory methods for objects that implement a particular interface, in the manner of java.util.Collections. Lastly, they can be used to group methods on a final class, instead of extending the class. Such utility classes were not designed to be instantiated: an instance would be nonsensical.

 

2.    A class can be made non-instantiable by including a private constructor.

 

3.    As a side effect, this idiom also prevents the class from being subclassed.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics