为什么System类声明为final并具有私有构造函数? [英] Why System class declared as final and with private constructor?

查看:404
本文介绍了为什么System类声明为final并具有私有构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解


最后一堂课是只是一个无法扩展的类。

A final class is simply a class that can't be extended.



具有单个无参数私有构造函数的类



A class with single no argument private constructor


除了同一个类中的表单之外,无法实例化具有私有构造函数的类。这使得从另一个类扩展它变得毫无用处。但这并不意味着它根本不能被分类,在内部类中我们可以扩展并调用私有构造函数。

A class with private constructors cannot be instantiated except form inside that same class. This make it useless to extend it from another class. But it does not mean it cannot be sub classed at all, among inner classes we can extend and call the private constructor.

所以我的理解是,如果我们用单个无参数私有构造函数创建一个类,那么将它们声明为最终并不意味着它们。然后为什么Java中的System类,虽然它有单个无参数的私有构造函数,但声明为final类?

So my understanding is, if we create a class with single no argument private constructor, their is no meaning to declare that class as final. Then why System class in Java, declared as final class although it has single no argument private constructor?

我听说制作一个class final有一些性能提升。这是正确的,这是将System类声明为final的唯一原因吗?请澄清为什么Java实现了这样的System类。

I heard that making a class final has some performance gain. Is this correct and is this the only reason to declare the System class as final? Please clarify me why Java implemented the System class like this.

推荐答案

有时,你需要一些实用程序类,其中包含一些静态实用方法。预计这些课程不会延长。因此,开发人员可能做出一些防御性决定并将这些类别标记为最终。我想说,用final标记Java类可能会导致Hotspot的性能略有改善(请参考 https://wikis.oracle.com/display/HotSpotInternals/VirtualCalls )。但是,我很确定,这是一个设计决定而不是性能。

Sometimes, you need some "utility" classes, which contain some static utility methods. Those classes are not expected to be extended. Thus, developers may make some defensive decisions and mark such classes "final". I would say, marking a Java class with "final" may cause a tiny little performance improvements in Hotspot (please refer to https://wikis.oracle.com/display/HotSpotInternals/VirtualCalls). But, I am pretty sure, that it was a design decision rather than performance.

如果你想拥有一些不可变的值对象,你可以使用final类,或者如果您只想通过工厂方法实例化类。在处理并发时,不可变值对象特别有用:

You can use final classes, if you want to have some immutable value objects as well, or if you want to make the classes be instantiated only through their factory methods. Immutable value objects are especially very useful while dealing with concurrency:

public final class MyValueObject {
   private final int value;
   private MyValueObject(int value) { this.value = value; }
   public static MyValueObject of(int value) { return new MyValueObject(value); }  
}

使用静态工厂方法,您可以隐藏所有这些构造细节,工厂方法。此外,您可以通过该工厂控制该类在运行时的实例数 - 如单例中一样。

Using the static factory method, you can hide all those construction details, behind the factory method. Moreover, you can control, through that factory, the number of instances of that class in runtime - as in singletons.

正如我所说,所有这些都是设计决定。我很确定最终课程没有带来显着的性能提升。

As I told, all these are design decisions. I am pretty sure that there is no remarkable performance gain brought by final classes.

这篇关于为什么System类声明为final并具有私有构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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