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)的更多相关文章

  1. C# empty private constructor

    A private constructor is a special instance constructor. It is generally used in classes that contai ...

  2. 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 ...

  3. Effective Java 04 Enforce noninstantiability with a private constructor

    A class can be made noninstantiable by including a private constructor. // Noninstantiable utility c ...

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

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

  5. 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)实现单例模式. ...

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

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

  7. 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 ...

  8. Add In 简介(主要翻译于ESRI官方文档)

    为ArcGIS桌面端建立Add In插件 平时以工作为主,有空时翻译一些文档,顺便练习英文,这个是因为用Add In来学习一下. 主要包括: 关于Add In 什么时候使用Add In Python ...

  9. 封装、构造方法、private、Static与this关键字、main()_Day07

    1:成员变量和局部变量的区别(理解) (1)定义位置区别:      成员变量:定义在类中,方法外.    局部变量:定义在方法中,或者方法声明上.    (2)初始化值的区别:   成员变量:都有默 ...

随机推荐

  1. 如何调用Http请求的接口

    /// <summary> /// 发起一个HTTP请求(以POST方式) /// </summary> /// <param name="url"& ...

  2. Oracle 自启动配置

    最后是放在rc.d/rc.local  里面,否则脚本编写错误 Linux有可能都启不来 [root@ChenJun-CentOS6 bin]# vi /etc/init.d/oracle #!/bi ...

  3. word中的域代码

    说明(2017-5-23 13:33:11): 1. Shift+F9显示域代码 2. Alt+F9显示全部域代码 3. Ctrl+F9添加一个域代码(一对大括号) 4. Ctrl+Shift+F9取 ...

  4. GridView“GridView1”激发了未处理的事件“RowDeleting”

    GridView“GridView1”激发了未处理的事件“RowDeleting”. 原因:1.模板列或者buttoncommand里的commandname=“Delete”,“Update”等关键 ...

  5. Visual Studio无法导航到插入点下面的符号

    Visual Studio2017编辑器按F12无法跳转到变量所属的类定义,弹窗提示[无法导航到插入点下面的符号],如下图: 解决办法: 方法一: 清理解决方案,重新生成. 方法二: 如果以上办法不行 ...

  6. PHP——大话PHP设计模式——链式操作

  7. iptables配置文件/etc/sysconfig/iptables内容详解

    #头两行是注释说明# Firewall configuration written by system-config-securitylevel# Manual customization of th ...

  8. CSS(九):设置盒子水平垂直居中

    通过设置下面的样式可以使盒子水平垂直居中: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  9. Spring Cloud 5分钟搭建教程(附上一个分布式日志系统项目作为参考) - 推荐

    http://blog.csdn.net/lc0817/article/details/53266212/ https://github.com/leoChaoGlut/log-sys 上面是我基于S ...

  10. Spring @Value注解问题

    xml配置了下面标签:<context:property-placeholder location="classpath:xxx.properties" /> 用spr ...