Java 为什么我们不能将顶级类定义为私有?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1913863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:50:17  来源:igfitidea点击:

Why can't we define a top level class as private?

java

提问by Satya

Why does Java not allow a top level class to be declared as private? Is there any other reason other than "We can't access a private class"?

为什么 Java 不允许将顶级类声明为私有?除了“我们无法访问私人课程”之外,还有其他原因吗?

采纳答案by Maximilian Mayerl

A top-level class as private would be completely useless because nothing would have access to it.

作为私有的顶级类将完全无用,因为没有人可以访问它。

回答by Guillaume

Java doesn't allow a top level class to be private. Only 'public' or 'package'.

Java 不允许顶级类是私有的。只有“公共”或“包”。

回答by Stephen C

In theory, you could instantiate and call methods on a privatetop-level class (if such a thing were allowed by the language ... which it isn't!), but you would have to use reflection to do this. Sensibly (IMO) Sun decided that private top-level classes were not a good thing to support at the language level.

理论上,您可以在private顶级类上实例化和调用方法(如果语言允许这样的事情......它不是!),但您必须使用反射来做到这一点。明智地 (IMO) Sun 认为,在语言级别支持私人顶级课程并不是一件好事。

Actually, it is possible that the JVM mightsupport top-level private "classes" created by bytecode magic. But it is not a useful thing to do.

实际上,JVM可能支持由字节码魔术创建的顶级私有“类”。但这不是一件有用的事情。



UPDATE- In fact, the current JVM specmakes it clear that the ACC_PRIVATE bit of the access flags word for a class is "reserved for future use", and that Java implementations should treat it as unset. Thus, the above speculation is moot for any JVM that strictly implements the current specification.

更新- 事实上,当前的 JVM 规范明确指出类的访问标志字的 ACC_PRIVATE 位是“保留供将来使用”,并且 Java 实现应将其视为未设置。因此,上述推测对于任何严格实现当前规范的 JVM 都没有实际意义。

回答by Ranger

There would be no way to access that class or its members.

将无法访问该类或其成员。

回答by stevebot

I believe a better question would be:

我相信一个更好的问题是:

What would it mean for a top level class to be private?

顶级课程是私人课程意味着什么?

If you think in terms of access levels, the level above class is package. In fact you can have package private top level classes in Java! Taking from the Oracle (formerly Sun) Java tutorials:

如果从访问级别来考虑,类之上的级别是包。事实上,您可以在 Java 中拥有包私有顶级类!取自Oracle(以前称为 Sun)Java 教程

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

如果一个类没有修饰符(默认设置,也称为包私有),它只在它自己的包中可见(包被命名为相关类的组——你将在后面的课程中了解它们。)

Depending on the answer to the question I asked, this might fit with your definition of a "top level private class".

根据我提出的问题的答案,这可能符合您对“顶级私人课程”的定义。

回答by Aniket Thakur

Private classes are allowed, but only as inner or nested classes. If you have a private inner or nested class, then access is restricted to the scope of that outer class.

允许私有类,但只能作为内部类或嵌套类。如果您有一个私有的内部类或嵌套类,则访问仅限于该外部类的范围。

If you have a private class on its own as a top-level class, then you can't get access to it from anywhere. So it does not make sense to have a top level private class.

如果您有一个私有类作为顶级类,那么您将无法从任何地方访问它。因此,拥有顶级私人课程是没有意义的。

回答by Rajnish

We can not declare an outer class as private. More precisely, we can not use the private access specifier with an outer class. As soon as you try to use the private access specifier with a class you will get a message in Eclipseas the error that only public, final, and abstractcan be used as an access modifier with a class.

我们不能将外部类声明为私有。更准确地说,我们不能在外部类中使用私有访问说明符。只要你尝试使用私人的访问符与类你会得到一个消息,Eclipse中的错误,只有publicfinalabstract可作为一类的访问修饰符。

Making a class private does not make any sense as we can not access the code of its class from the outside.

将类设为私有没有任何意义,因为我们无法从外部访问其类的代码。

回答by Timothy Perez

You cannot define a top level class private (or anything else besides public). You will get a compilation error.

您不能定义顶级类私有(或除公共之外的任何其他内容)。你会得到一个编译错误。

Something.java:6: error: modifier private not allowed here
private class Something {
        ^
1 error

You have only two options, publicor no access modifier at all. By omitting public you, implicitly, limit class access to within the package (aka: package-private).

您只有两个选项,public或根本没有访问修饰符。通过省略 public,您可以隐式地限制对包内的类访问(又名:包私有)。

回答by Nikhil Arora

This needs a simple understanding of why it is not required to declare classes as private in Java.

这需要简单理解为什么在 Java 中不需要将类声明为私有的。

If your class is itself a standalone program/software (which is highly unlikely) then you would have already defined it in a project and a package specific for it. So adding the privatekeyword is redundant to it.

如果您的类本身是一个独立的程序/软件(这极不可能),那么您已经在项目和特定于它的包中定义了它。所以添加private关键字是多余的。

If that's not the case then default access is then it means your program/software depends on different classes to run. If we are declaring one class among them as private (in case if we could) then we are restricting its accessibility by other classes, etc. which isn't of any use. It simply means that the class declared as private by us isn't of any use for the code to run. Which again renders it useless.

如果不是这种情况,那么默认访问是那么这意味着您的程序/软件依赖于不同的类来运行。如果我们将其中的一个类声明为私有类(以防万一),那么我们就会限制其他类等对它的可访问性,这没有任何用处。这只是意味着我们声明为私有的类对运行代码没有任何用处。这再次使它变得无用。

If you mean package level access then for default access we don't need to declare any keyword before it.

如果您的意思是包级访问,那么对于默认访问,我们不需要在它之前声明任何关键字。

回答by Naresh Joshi

As we already know, a field defined in a class using the privatekeyword can only be accessible within the same class and is not visible to the outside world.

我们已经知道,使用private关键字在类中定义的字段只能在同一个类中访问,对外界不可见。

So what will happen if we will define a class private? Will that class only be accessible within the entity in which it is defined which in our case is its package?

那么如果我们定义一个私有类会发生什么?该类是否只能在定义它的实体中访问,在我们的例子中是它的包?

Let's consider the below example of class A

让我们考虑以下 A 类示例

package com.example;
class A {
    private int a = 10;

    // We can access a private field by creating object of same class inside the same class
    // But really nobody creates an object of a class inside the same class
    public void usePrivateField(){
        A objA =  new A();
        System.out.println(objA.a);
    }
}

Field ‘a' is declared as private inside ‘A' class and because of it, the ‘a' field becomes private to class ‘A' and can only be accessed within ‘A'. Now let's assume we are allowed to declare class ‘A' as private, so in this case class ‘A' will become private to package ‘com.example' and will not be accessible from outside of the package.

字段 'a' 在 'A' 类中被声明为私有,因此,'a' 字段成为类 'A' 的私有字段,并且只能在 'A' 内访问。现在让我们假设我们可以将类 'A' 声明为私有类,因此在这种情况下,类 'A' 将成为包 'com.example' 的私有类,并且不能从包外部访问。

So defining private access to the class will make it accessible inside the same package which the defaultkeyword already does for us. Therefore there isn't any benefit of defining a class private; it will only make things ambiguous.

因此,定义对类的私有访问将使其可以在default关键字已经为我们所做的同一个包中访问。因此,定义一个私有类没有任何好处;它只会让事情变得模棱两可。

You can read more in my article Why an outer Java class can't be private or protected.

您可以在我的文章Why an external Java class can't be private or protected 中阅读更多内容。