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)初始化值的区别: 成员变量:都有默 ...
随机推荐
- mac下增加eclipse内存
在mac上找不到eclipse.ini文件编辑内存限制,在eclipse安装目录右击eclipse程序,选“显示包内容”,eclipse.ini就在 Content/MacOS下.
- 【转】asp.net中@page指令的属性Inherits、Src、CodeBehind区别
Inherits.Src.CodeBehind 在 ASP.NET 中使用代码隐藏方法来设计Web 窗体,可使页代码能够更清晰地从 HTML 内容中分离到完全单独的文件中. 通常一个 @page 指令 ...
- DIV+CSS网页设计规范
1.网页HTML代码最前面必须包括W3C声明,以便符合标准: 一般网页: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...
- 开发错误处理记录(无法激活服务,因为它不支持 ASP.NET 兼容性)
错误提示:无法激活服务,因为它不支持 ASP.NET 兼容性.已为此应用程序启用了 ASP.NET 兼容性.请在 web.config 中关闭 ASP.NET 兼容性模式或将 AspNetCompat ...
- 项目抛弃Tomcat容器,用代码启动Tomcat插件
tomato启动代码如下: package tomcat; import org.apache.catalina.connector.Connector; import org.apache.cata ...
- mysql行转列转换
http://blog.csdn.net/sinat_27406925/article/details/77507478 mysql 行列转换 ,在项目中应用的极其频繁,尤其是一些金融项目里的报表.其 ...
- for语句练习 阶乘
4的阶乘:4!=1*2*3*4 public class g { /** * @param args */ public static void main(String[] args) { int n ...
- Spark硬件配置推荐
1.存储系统 如果可以的话,把Spark的hadoop的节点安装在一起,最容易的方式是用standalone的模式安装,用mapred.child.java.opts设置每个任务的内存,用mapred ...
- android开发(43) 动画演示,会跑的小人,从屏幕左侧跑到右侧
想做一个动画,一个会跑的小人,从屏幕右侧跑道右侧,于是做了个尝试,上图: 要完成这样需要三步: 1. 做一个 帧动画 (frame animation),由多张图片组成,组成小人连续跑动的样子. 2. ...
- Materialize快速入门教程
https://materializecss.com/ https://github.com/Dogfalo/materialize http://www.materializecss.cn/ 1,下 ...