Java 单个文件中的多个类:此处不允许使用修饰符私有
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3491131/
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
multiple classes in a single file : modifier private not allowed here
提问by Jaguar
I am not able to understand why this code doesn't compile:
我不明白为什么这段代码不能编译:
class A {
public static void main(String[] args) {
System.out.println("hi");
}
}
private class B {
int a;
}
I am saving the contents in a file named A.java
- and I get an error:
我将内容保存在名为A.java
-的文件中,但出现错误:
modifier private not allowed here // where I have defined class B
This happens both when I try B as private and protected. Can someone please explain me the reason behind this?
当我尝试将 B 设为私有和受保护时,都会发生这种情况。有人可以解释一下这背后的原因吗?
Thanks !
谢谢 !
采纳答案by Andreas Dolk
From the Java Language specification:
The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration
访问修饰符 protected 和 private 仅适用于直接封闭类声明中的成员类
So yes, the private and the protected modifiers are not allowed for top level class declarations.
所以是的,私有和受保护的修饰符不允许用于顶级类声明。
Top-levelclasses may be public or not, while private
and protected
are not allowed. If the class is declared public, then it can be referred to from any package. Otherwise it can only be referred to from the same package (namespace).
顶级类可以是公共的或没有,而private
并protected
不允许。如果类被声明为 public,则可以从任何包中引用它。否则只能从同一个包(命名空间)中引用。
A private top level class wouldn't make much sense because it couldn't be referred to from any class. It would be unusable by definition. private
is OK for member classes to make a class referable to only it's enclosing class.
私有顶级类没有多大意义,因为它不能从任何类中引用。根据定义,它将无法使用。private
成员类可以将一个类仅引用到它的封闭类。
A protected member class can be referred to from (1) any class of the same package and from (2) any subclass of the enclosing class. Mapping this concept to top level classes is difficult. The first case is covered by top level class with no access modifiers. The second case is not applicable for top level classes, because there is no enclosing class or something else from a different package with a special relation to this class (like a subclass). Because of this I think, protected
is not allowed because it's underlying concept is not applicable for top level classes.
受保护的成员类可以从 (1) 同一包的任何类和 (2) 封闭类的任何子类引用。将这个概念映射到顶级类是很困难的。第一种情况由没有访问修饰符的顶级类覆盖。第二种情况不适用于顶级类,因为没有封闭类或来自与此类具有特殊关系的不同包的其他东西(如子类)。正因为如此,我认为protected
是不允许的,因为它的底层概念不适用于顶级类。
回答by djna
Just have no private/protected modifier at all.
根本没有私有/受保护的修饰符。
回答by linuxuser27
B needs to be private to something. Place it within the definition of class A or create another file, B.java, and define it there, but then it cannot be private.
B 需要对某事保密。将它放在类 A 的定义中或创建另一个文件 B.java,并在那里定义它,但它不能是私有的。
回答by Konstantin Burov
Make the B nested of A, like this:
使 B 嵌套在 A 中,如下所示:
class A {
public static void main(String[] args) {
System.out.println("hi");
}
private class B {
int a;
}
}
Or move B to a separate file. Also you can stick with default access level, this way the class can be accessed only from within the package:
或将 B 移至单独的文件。您也可以坚持使用默认访问级别,这样只能从包内访问该类:
class A {
public static void main(String[] args) {
System.out.println("hi");
}
}
class B {
int a;
}
回答by egbokul
If you don't use the public keyword for the class it will be private by default (visible only within the file).
如果您不为类使用 public 关键字,默认情况下它将是私有的(仅在文件中可见)。
There can be only one public class per .java file, all the others need to be private. So class A can be public and class B doesn't need any modifiers in your example. The public class name must match the .java file name (eg. A.java can only contain one public class called "A").
每个 .java 文件只能有一个公共类,所有其他类都需要是私有的。因此,类 A 可以是公共的,而类 B 在您的示例中不需要任何修饰符。公共类名称必须与 .java 文件名匹配(例如,A.java 只能包含一个名为“A”的公共类)。
回答by fastcodejava
A.java
cannot contain two classes.
A.java
不能包含两个类。
回答by Venkataswamy
private and protected are meaningless to be allowed to a top level(not member) class/interface.
private 和 protected 被允许用于顶级(非成员)类/接口是没有意义的。
They are applicable only to the class members which can be variables, constants, constructors, methods, classes, and interfaces.
它们仅适用于可以是变量、常量、构造函数、方法、类和接口的类成员。
Why:
为什么:
(1) private:What may be the meaning/purpose if we define a class as private. Its scope should be private to some area. default access is already package private. And nobody wants a class to be source file private, (Guessing the reason) it may not be a good programming practice to allow because java applications are finally organized in the form of packages, but not in terms of source files. Any source file should be part of some package, so in broad/final view each class/interface is part of some package, not just of some .java file. So not applicable.
(1)私有的:如果我们将一个类定义为私有的,它的意义/目的是什么。它的范围应该是某个区域的私有范围。默认访问权限已经是包私有的。并且没有人希望一个类是源文件私有的,(猜测原因)这可能不是一个好的编程实践,因为 Java 应用程序最终以包的形式组织,而不是以源文件的形式组织。任何源文件都应该是某个包的一部分,所以在广义/最终视图中,每个类/接口都是某个包的一部分,而不仅仅是某个 .java 文件的一部分。所以不适用。
(2) protected: If something is protected it should be available only within package and only to the sub classes in other packages. To extend a class in a different package, it should be available to all the classes in other packages, but protected says class should be available only to the classes extended it. It's a kind of deadlock situation. So not applicable.
(2) protected:如果某个东西是受保护的,它应该只在包内可用,并且只对其他包中的子类可用。要在不同的包中扩展一个类,它应该对其他包中的所有类可用,但是 protected 表示类应该只对扩展它的类可用。这是一种僵局。所以不适用。
Source: My readings and understanding
资料来源:我的阅读和理解