Effective Java 04 Enforce noninstantiability with a private constructor
2014-02-26 22:33 小郝(Kaibo Hao) 阅读(486) 评论(0) 编辑 收藏 举报A class can be made noninstantiable by including a private constructor.
// Noninstantiable utility class
public class UtilityClass {
// Suppress default constructor for noninstantiability
private UtilityClass() {
throw new AssertionError();
}
... // Remainder omitted
}
Advantage
This explicitly prevents the user to instantiate the class.
Disadvantage
The class cannot be subclassed.
作者:小郝
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。