add a private constructor to hide the implicit public one(Utility classes should not have public constructors)
sonarlint提示add a private constructor to hide the implicit public one
Utility classes should not have public constructors
Utility classes, which are collections of static members, are not meant to be instantiated. Even abstract utility classes, which can be extended, should not have public constructors.
Java adds an implicit public constructor to every class which does not define at least one explicitly. Hence, at least one non-public constructor should be defined.
Noncompliant Code Example
class StringUtils { // Noncompliant public static String concatenate(String s1, String s2) {
return s1 + s2;
} }
Compliant Solution
class StringUtils { // Compliant private StringUtils() {
throw new IllegalStateException("Utility class");
} public static String concatenate(String s1, String s2) {
return s1 + s2;
} }
Exceptions
When class contains public static void main(String[] args) method it is not considered as utility class and will be ignored by this rule.
意思是util类里面都是静态方法,不会去初始化这个类,所以不应该暴露一个public构造函数
解决方案:
定义一个private构造函数
add a private constructor to hide the implicit public one(Utility classes should not have public constructors)的更多相关文章
- C# empty private constructor
A private constructor is a special instance constructor. It is generally used in classes that contai ...
- 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 ...
- Effective Java 04 Enforce noninstantiability with a private constructor
A class can be made noninstantiable by including a private constructor. // Noninstantiable utility c ...
- Effective Java Item4:Enforce noninstantiability with a private constructor
Item4:Enforce noninstantiability with a private constructor 通过构造私有化,禁止对象被实例化. public class UtilClass ...
- 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)实现单例模式. ...
- Java之创建对象>4.Enforce noninstantiability with a private constructor
如果你定义的类仅仅是包含了一些静态的方法和静态的字段,这些类一般是一些工具类,这些一般是设计为不能被实例化的. 1. Attempting to enforce noninstantiability ...
- Java之创建对象>3.Enforce the singleton property with a private constructor or an enum type
1. 通过一个公开的字段来获取单例 // Singleton with public final field public class Elvis { public static final Elv ...
- Add In 简介(主要翻译于ESRI官方文档)
为ArcGIS桌面端建立Add In插件 平时以工作为主,有空时翻译一些文档,顺便练习英文,这个是因为用Add In来学习一下. 主要包括: 关于Add In 什么时候使用Add In Python ...
- 封装、构造方法、private、Static与this关键字、main()_Day07
1:成员变量和局部变量的区别(理解) (1)定义位置区别: 成员变量:定义在类中,方法外. 局部变量:定义在方法中,或者方法声明上. (2)初始化值的区别: 成员变量:都有默 ...
随机推荐
- poj1182(种类并查集好题)
不得不说,我得感谢@驱动幽灵百鬼夜行小肆,正是因为看明白了他给出的解析,我才完全弄懂种类并查集的,这里,我也不想去改其他的,就直接引用他的解题报告吧 转载:http://blog.csdn.net/c ...
- 正益无线首页jQuery焦点图
分享一款正益无线首页jQuery焦点图,带索引按钮,自动轮播切换特效焦点图代码. 在线预览 源码下载 实现的代码. html代码: <div id="slideBox" ...
- 基于jQuery环形图标菜单旋转切换特效
分享一款基于jQuery环形图标旋转切换特效.这是一款鼠标点击图标菜单圆形顺时针或者逆时针旋转切换代码.效果图如下: 在线预览 源码下载 实现的代码. js代码: /* 图片地址可以是相对路径或绝 ...
- win7+python3.6+word_cloud 安装出现Microsoft Visual C++ 14.0 is required
说明 环境: 已安装Anaconda3 (64-bit) 4.4.0(Python 3.6.1).其中,代码调试在Spyder 3.1.4中进行,安装包则直接打开Anaconda Prompt调用cm ...
- laravel服务l队列资料整理
Laravel 队列系列 —— 基于 Redis 实现任务队列的基本配置和使用 1.概述 在Web开发中,我们经常会遇到需要批量处理任务的场景,比如群发邮件.秒杀资格获取等,我们将这些耗时或者高并发的 ...
- PopupWindow 常用方法学习笔记
PopupWindow可以实现浮层效果,而且可以自定义显示位置,出现和退出时的动画. 首先解析一个View LayoutInflater inflater = getLayoutInflater(); ...
- DLL入门
DllTest工程 QMath.h #pragma once #ifdef API_EXPORT #define DLL_CLASS __declspec(dllexport) #define DLL ...
- 使用taskset命令来限制进程的CPU
常常感觉系统资源不够用,一台机子上跑了不下3个比较重要的服务,但是每天我们还要在上面进行个备份压缩等处理,网络长时间传输,这在就很影响本就不够用的系统资源: 这个时候我们就可以把一些不太重要的比如co ...
- Linux/Unix下的任务管理器-top命令
Windows下的任务管理器虽然不好用(个人更喜欢Process Explorer些),但也算方便,可以方便的查看进程,CPU,内存...也可以很容易的结束进程 没有图形化界面下的Linux,也有命令 ...
- drupal8 管理入门
https://www.drupal.org/node/1896670 本节将介绍新的Drupal8用户,网站管理.它涵盖了使用管理员帐户,并提供从哪里开始的建议. 了解管理员帐户 在安装过程结束时, ...