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. shell格式化字符串

    假如你有以下代码: TEMP_SQL="SELECT count(uid) from ${TABLE_PREFIX}_%s;" SUM= for((i=${MIN};i<${ ...

  2. Django套用现成模板,导入css,js,images等文件

    https://blog.csdn.net/mildddd/article/details/79557803

  3. MySQL 中联合查询效率分析

    目前我有两个表,一个keywords和一个news表.keyword存放关键词是从news中提取,通newsid进行关联,两表关系如图: keywords中存有20万条数据,news中有2万条数据,现 ...

  4. 使用spring-rabbit测试RabbitMQ消息确认(发送确认,接收确认)

    1.首先是rabbitmq的配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  5. iOS手机号,身份证,车牌号正则表达式

    1.手机号判断,根据维基百科2016年6月修订的段号判断 是否是手机号 /** 手机号码 13[0-9],14[5|7|9],15[0-3],15[5-9],17[0|1|3|5|6|8],18[0- ...

  6. /.well-known/apple-app-site-association

    Technical Q&A QA1919 Incoming requests for /.well-known/apple-app-site-association file Q:  Why ...

  7. C语言 · x的x次幂结果为10

    如果x的x次幂结果为10(参见[图1.png]),你能计算出x的近似值吗? 显然,这个值是介于2和3之间的一个数字. 请把x的值计算到小数后6位(四舍五入),并填写这个小数值. 注意:只填写一个小数, ...

  8. 字符串过滤掉所有最邻近的“<”和“>”之间的字符

    请编写一个方法,实现如下功能:请输入字符串过滤掉所有最邻近的“<”和“>”之间的字符,将其与字符返回. 例如:输入<html><body>4<5<123 ...

  9. Android——selector背景选择器的使用详解(二)

    在开发应用中,很多情况下要设计listview或button控件的背景,下面总结一下android的selector的用法:1.在drawable中配置Android的selector.将如下的XML ...

  10. CSS(三):引入样式和优先级

    CSS的引入样式总共有三种:行内样式(内联样式表).内部样式表.外部样式表.下面分别来介绍这三种样式. 一.行内样式 行内样式也叫内联样式,使用style属性引入CSS样式.看下面的示例: <! ...