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.

Effective Java 04 Enforce noninstantiability with a private constructor的更多相关文章

  1. Effective Java Item4:Enforce noninstantiability with a private constructor

    Item4:Enforce noninstantiability with a private constructor 通过构造私有化,禁止对象被实例化. public class UtilClass ...

  2. Java之创建对象>4.Enforce noninstantiability with a private constructor

    如果你定义的类仅仅是包含了一些静态的方法和静态的字段,这些类一般是一些工具类,这些一般是设计为不能被实例化的. 1. Attempting to enforce noninstantiability ...

  3. Effective Java 03 Enforce the singleton property with a private constructor or an enum type

    Principle When implement the singleton pattern please decorate the INSTANCE field with "static ...

  4. Effective Java Item3:Enforce the singleton property with a private constructor or an enum type

    Item3:Enforce the singleton property with a private constructor or an enum type 采用枚举类型(ENUM)实现单例模式. ...

  5. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  6. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  7. 《Effective Java》读书笔记 - 2.创建和销毁对象

    Chapter 2 Creating and Destroying Objects item 1:Consider static factory methods instead of construc ...

  8. Java之进阶(1) -《Effective Java》

    第1章 引言 第2章 创建和销毁对象 第1条:考虑用静态工厂方法代替构造器(Consider static factory methods instead of constructors) 第2条:遇 ...

  9. Effective Java 第三版——4. 使用私有构造方法执行非实例化

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

随机推荐

  1. Android 学习笔记之AndBase框架学习(一) 实现多功能标题栏

    PS:Volley框架终于通过看源码的方式完成了所有的学习..开始学习AndBase...AndBase的源码实在是多的离谱...因此就不对所有的源码进行分析了... 学习内容: 1.使用AndBas ...

  2. 透过WebGL 3D看动画Easing函数本质

    50年前的这个月诞生了BASIC这门计算机语言,回想起自己喜欢上图形界面这行,还得归功于当年在win98下用QBASIC照葫芦画瓢敲了一段绘制奥运五环的代码,当带色彩的奥运五环呈现在自己面前时我已知道 ...

  3. [JS] JavaScript框架(2) D3

    D3(Data-Driven Documents)是一个用于网页作图.生成互动图形的JavaScript函数库. 官网:http://d3js.org/ 下载: cdn:<script src= ...

  4. 12 19 spring3 项目总结

    项目截图: 林集团 https://home.cnblogs.com/u/linjituan/ 团队guihub: https:///github.com/MoiGi 李鹏飞 https://www. ...

  5. 【循序渐进学Python】7.面向对象的核心——类型(上)

    我们知道Python是一门面向对象的脚本语言.从C#的角度来看:首先Python支持多继承.Python 类型成员通常都是public的,并且所有成员函数都是virtual的(可以直接重写). 1. ...

  6. WebApi传参总动员(二)

    上篇,从最简单的string入手.本篇演示了从请求的输入流中获取实体.api: public class ValuesController : ApiController { [HttpPost] p ...

  7. string.join加引号

    columnsGen = string.Join(",", modelDictionary.Keys); valueGen = modelDictionary.Values.Agg ...

  8. YEdit

    YEdit YEdit is a YAML editor for Eclipse. See the wiki for more details Installation Use the Eclipse ...

  9. HDU 2159---FATE---带限制的完全背包

    HDU   2159 Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一 ...

  10. 后缀数组---Milk Patterns

    POJ  3261 Description Farmer John has noticed that the quality of milk given by his cows varies from ...