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. 内核参数SEMMSL SEMMNS SEMOPM SEMMNI参数的设置

    内核参数SEMMSL SEMMNS SEMOPM SEMMNI参数的设置  转自:http://www.dbafree.net/?p=92   这四个参数自己一直没搞清楚 今天问了下同事,大概整了一下 ...

  2. hadoop的五个守护进程【转】

    hadoop的五个守护进程  [转自]:http://xubindehao.iteye.com/blog/1395580 一般如果正常启动hadoop,我们可以在master上通过jps命令看到以下5 ...

  3. LVS之NAT和DR服务脚本

    NAT服务控制脚本 #!/bin/bash # # chkconfig: - 88 12 # description: LVS script for VS/NAT # . /etc/rc.d/init ...

  4. 简洁的python测试框架——Croner

    [本文出自天外归云的博客园] Croner简介 这是一个非常简洁的测试框架,是基于python3的nose进行二次开发的. Github地址 可以随意的在此基础上进行扩展以支持jenkins等其他扩展 ...

  5. lsblk命令

    lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息.块设备有硬盘,闪存盘,cd-ROM等等.lsblk命令包含在util-linux-ng包中,现 ...

  6. hadoop 大数据 介绍

    1.Hadoop是一个大家族,是一个开源的生态系统,是一个分布式运行系统,是基于Java编程语言的架构.不过它最高明的技术还是HDFS和MapReduce,使得它可以分布式处理海量数据. 2.HDFS ...

  7. Linux使用redis

    在linux遇到这种情况. 注意,这里本redis 用的端口是6389 通过 ps -aux 看到redis 启动了: root ? Ssl Jun14 : redis-server *: root ...

  8. JAVA传入一个字符串,返回一个字符串中的大写字母

    /**      *       * @param 传入一个字符串      * @return 返回一个字符串中的大写字母      */     private static String str ...

  9. for语句查看js对象

    for (var obj in o.curform) { document.write("<p>" + obj + " = " + o.curfor ...

  10. java jar包与配置文件的写法

    一个普通的java project,里面引用了config.properties配置文件,将项目打成Runnable jar,然后将config.properties放到打包后的jar路径下,执行该j ...