Utility classes should not have public constructors
Utility classes should not have public constructors
Utility classes, which are collections of static
members, are not meant to be instantiated.
C# adds an implicit public constructor to every class which does not explicitly define at least one constructor. Hence, at least one protected
constructor should be defined if you wish to subclass this utility class. Or the static
keyword should be added to the class declaration to prevent subclassing.
Noncompliant Code Example
public class StringUtils // Noncompliant { public static string Concatenate(string s1, string s2) { return s1 + s2; } }
Compliant Solution
public static class StringUtils { public static string Concatenate(string s1, string s2) { return s1 + s2; } }
or
public class StringUtils { protected StringUtils() { } public static string Concatenate(string s1, string s2) { return s1 + s2; } }
Is it mandatory utility class should be final and private constructor?
This is not a mandate from functional point of view or java complication or runtime. However, its coding standard accepted by wider community. Even lot of static code review tools like checkstyle and many others checks that such classes have this covention followed.
Why this convention is followed , is already explained in other answers and even OP covered that.
I like to explain it little further , mostly Utility classes have the methods/functions which are independent of object instance. Those are kind of aggregate functions.As they depend only on parameters for return values and not associated with class variables of utility class. So, mostly these functions/methods are kept static. As a result, Utility classes are ideally classes with all the static methods. So, any programmer calling these methods don't need to instantiate this class. However, some robo-coders (may be with less experience or interest) will tend to create object as they believe they need to before calling its method. To avoid that creating object, we have 3 options :-
Keep educating people don't instantiate it. (No sane person can keep doing it.)- Mark class as abstract :- Again now robo-coders will not create object. However, reviewes and wider java community will argue that marking abstract means you want someone to extend it. So, this is also not good option.
- Private constructor :- Protected will again allow the child class to create object.
Now, again if someone wants to add new method for some functionality to these utility class , he don't need to extend it , he can add new method as each method is indepenent and no chance of breaking other functionalities. So, no need to override it. And also you are not going to instiantiate, so need to subclass it. Better to mark it final.
In summary , Creating object of utility classes does not make sense. Hence the constructors should either be private. And you never want to override it ,so mark it final.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-06-02 RadioButton的check改变的时候
2017-06-02 Docs-->.NET-->API reference-->System.Web.UI.WebControls-->Repeater
2017-06-02 Docs-->.NET-->API reference-->System.Web.UI-->Control-->Methods-->FindControl
2017-06-02 通过Debug-->Attach to Process的方式来调试网站
2017-06-02 Tools-->SQL Server Profiler监视数据库
2017-06-02 WebForms简介
2016-06-02 Don't Block on Async Code