它是强制性的实用工具类,应该是最终的私有构造函数吗? [英] Is it mandatory utility class should be final and private constructor?

查看:120
本文介绍了它是强制性的实用工具类,应该是最终的私有构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过创建私有构造函数,我们可以避免在外部任何地方实例化类。通过将班级定为最终班级,其他任何班级都无法对其进行扩展。为什么Util类必须具有 private 构造函数和 final 类?

By making private constructor, we can avoid instantiating class from anywhere outside. and by making class final, no other class can extend it. Why is it necessary for Util class to have private constructor and final class ?

推荐答案

从功能的角度或Java复杂性或运行时来看,这不是强制性的。但是,其编码标准已为更广泛的社区所接受。甚至许多静态代码检查工具,例如 checkstyle 以及许多其他工具,都检查此类是否具有这种约定

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.

为什么遵循此约定,已经在其他答案中进行了说明,甚至OP也对此进行了解释。

Why this convention is followed , is already explained in other answers and even OP covered that.

我想进一步解释一下,大多数Utility类具有与对象实例无关的方法/函数。这些是聚合函数,因为它们仅依赖于返回值的参数,而不与实用程序类的类变量相关联。因此,大多数这些功能/方法保持静态。因此,实用程序类最好是所有静态方法的类。
因此,任何调用这些方法的程序员都无需实例化此类。但是,某些自动编码器(可能经验不足或兴趣不足)将倾向于创建对象,因为他们认为在调用其方法之前需要这样做。为避免创建该对象,我们提供3个选项:-

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 :-


  1. 继续教育人们不要实例化它。。 (没有理智的人可以继续这样做。)

  2. 将类标记为抽象:-
    现在,自动编码器将不再创建对象。但是,评论家和更广泛的Java社区将争辩说,标记抽象意味着您希望有人对其进行扩展。因此,这也不是一个好选择。

  3. 私有构造函数:-
    受保护将再次允许子类创建对象。

  1. Keep educating people don't instantiate it. (No sane person can keep doing it.)
  2. 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.
  3. 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.

总而言之,创建实用工具类的对象没有任何意义。因此,构造函数应该是私有的。而且您永远都不想覆盖它,因此将其标记为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.

这篇关于它是强制性的实用工具类,应该是最终的私有构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆