[英]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种选择:-
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类编译为字节码时,没有访问限制的概念, public
, package
, protected
, private
等效。 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.