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. java连接https时禁用证书验证.

    import java.io.File; import java.security.cert.CertificateException; import java.util.List; import j ...

  2. 【机器学习】粗糙集(Rough Set Approach)

    粗糙集理论是一种研究不精确,不确定性知识的数学工具. 粗糙集理论的知识表达方式一般采用信息表或称为信息系统的形式,它可以表现为四元有序组K=(U,A,V,P).其中U为对象的全体,即论域:A是属性全体 ...

  3. 微服务之springCloud-config-bus(十三)

    简介 当我们的业务系统越来越庞大复杂的时候,各种配置就会层出不群.一旦配置修改了,那么我们就是必须修改后停服务,然后再上线,如果服务少,我们可以手动来操作,如果是成千上百的服务,如果是手动操作,肯定就 ...

  4. client version is higher than daemon version (client is v.1.29 daemon is v.1.22)

    安装好coreseek,建了索引,启动了服务,用php建了一个test.php,用于测试:<?phpinclude_once('sphinxapi.php');//向搜索引擎发起请求 $cl = ...

  5. android sqlite应用优化(资料整理)

    1.  优化插入速度 a.不要绑定空列    在我的程序,至少有50%的列是空值.碰到空值列,就不调用ih.bind()方法对它进行绑定,就我的程序而言,当列值为null或者空的字符串是, 有将近30 ...

  6. C#中的volatile关键字

    volatile 关键字指示一个字段可以由多个同时执行的线程修改. 声明为 volatile 的字段不受编译器优化(假定由单个线程访问)的限制. 这样可以确保该字段在任何时间呈现的都是最新的值. vo ...

  7. AJAX 简单例程示例

    index.html <html> <head> <script> function showHint(str) { if (str.length==0) { re ...

  8. Python MQTT订阅获取发布信息字典过滤

    起因是因为 订阅的时候,获取到的 MQTT 信息时,第一条信息好像是连接信息,所以需要过滤他. 接收到的数据如下 必须要过滤这个 name : 1 的字典,操作如下: def on_message(c ...

  9. Face Alignment by Coarse-to-Fine Shape Searching--解析

    人脸关键点定位.Face Alignment by Coarse-to-Fine Shape Searching 算法源码详解(上) http://blog.csdn.net/shenxiaolu19 ...

  10. Selenium (2) —— Selenium WebDriver + Grid2(101 Tutorial)

    Selenium (2) -- Selenium WebDriver + Grid2(101 Tutorial) jvm版本: 1.8.0_65 selenium版本: v2.48.0 (Standa ...