首页 > 其他 > 详细

代码与流程规范

时间:2019-05-22 19:10:42      阅读:112      评论:0      收藏:0      [点我收藏+]
Utility classes should not have public constructors
 
Code smell
 
Major
squid:S1118
 
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.

代码与流程规范

原文:https://www.cnblogs.com/fonxian/p/10907409.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!