简体   繁体   English

它是强制性的实用程序类,应该是最终的私有构造函数吗?

[英]Is it mandatory utility class should be final and private constructor?

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 ? 为什么Util类必须具有private构造函数和final类?

This is not a mandate from functional point of view or java complication or runtime. 从功能的角度或Java复杂性或运行时来看,这不是强制性的。 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. 甚至很多静态代码查看工具(如checkstyle)和许多其他工具都检查此类遵循了这种约定。

Why this convention is followed , is already explained in other answers and even OP covered that. 为什么遵循该约定,已经在其他答案中进行了解释,甚至OP都对此进行了说明。

I like to explain it little further , mostly Utility classes have the methods/functions which are independent of object instance. 我想进一步解释一下,大多数Utility类具有与对象实例无关的方法/函数。 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 :- 为了避免创建该对象,我们有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. 但是,评论家和更广泛的Java社区将争辩说,标记抽象意味着您希望有人对其进行扩展。 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. 最好将其标记为最终。

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's not necessary, but it is convenient. 没必要,但是很方便。 A utility class is just a namespace holder of related functions and is not meant to be instantiated or subclassed. 实用程序类只是相关功能的名称空间持有者,并不意味着要实例化或子类化。 So preventing instantiation and extension sends a correct message to the user of the class. 因此,防止实例化和扩展会向该类的用户发送正确的消息。

There is an important distinction between the Java Language, and the Java Runtime. Java语言和Java运行时之间有一个重要的区别。

When the java class is compiled to bytecode, there is no concept of access restriction, public , package , protected , private are equivalent. 将java类编译为字节码时,没有访问限制的概念, publicpackageprotectedprivate等效。 It is always possible via reflection or bytecode manipulation to invoke the private constructor, so the jvm cannot rely on that ability. 总是可以通过反射或字节码操作来调用private构造函数,因此jvm无法依赖该功能。

final on the other hand, is something that persists through to the bytecode, and the guarantees it provides can be used by javac to generate more efficient bytecode, and by the jvm to generate more efficient machine instructions. 另一方面, final是一直存在到字节码中的东西,它提供的保证可以被javac用来生成更有效的字节码,而jvm可以用来生成更有效的机器指令。

Most of the optimisations this enabled are no longer relevant, as the jvm now applies the same optimisations to all classes that are monomorphic at runtime—and these were always the most important. 启用的大多数优化不再相关,因为jvm现在将相同的优化应用于在运行时是单态的所有类,而这些优化始终是最重要的。

默认情况下,此类通常用于聚合执行不同操作的函数,在这种情况下,我们无需创建新对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在最终(实用程序)类中模拟私有静态方法 - Mocking private static method in a final (utility) class Java Final 类或私有构造函数 - Java Final class or private constructor 从最终类(实用程序类)模拟私有静态方法 - Mocking private static method from a final class (utility class) 带有私有构造函数的final类,设计原理是什么 - Final class with private constructor, what is the design principle 为什么在Singleton类中使用私有Constructor是强制性的 - Why is it mandatory to have private Constructor inside a Singleton class 在私有实用程序类构造函数中使用的首选 Throwable 是什么? - What is the preferred Throwable to use in a private utility class constructor? 没有值的java枚举与私有构造函数的实用程序类之间的区别 - Difference between java enum with no values and utility class with private constructor 私有构造函数和最终 - private constructor and final 将类作为final并将类构造函数设置为private是有区别的 - What is the difference between having a class as final and having a class constructor as private 如何为sonarqube中的最终课程的私有构造函数提供测试覆盖范围? - How to give test coverage for private constructor of a final class in sonarqube?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM