没有公共类的 .java 文件的 Java 编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7633631/
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-10-30 20:46:38  来源:igfitidea点击:

Java compilation of a .java file without a public class

java

提问by anon1981

Okay, so a java source file musthave at least one public class and the file should be called "class-name.java". Fair enough.

好的,所以一个 java 源文件必须至少有一个公共类,并且该文件应该被称为“class-name.java”。很公平。

Hence, if I have a class, then the following would compile:

因此,如果我有一个类,那么以下将编译:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

But what bugs me is that if I remove the 'public' access modifier from the above code, the code still compiles. I just don't get it. Removing it, the code looks like:

但是让我感到困扰的是,如果我从上面的代码中删除“公共”访问修饰符,代码仍然可以编译。我只是不明白。删除它,代码如下所示:

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

In the above code, since I removed the publicaccess modifier, my class has defaultor packageaccess, i.e. it can't be accessed from the outside world, only from within the package.

在上面的代码中,由于我删除了public访问修饰符,我的类具有defaultorpackage访问权限,即无法从外部世界访问它,只能从包内部访问。

So my question is, how does the above code compile ? The file HelloWorld.javain this case does not have a publicHelloWorld class (only a package-private HelloWorld.class) and thus to my understanding should not compile.

所以我的问题是,上面的代码是如何编译的?HelloWorld.java这种情况下的文件没有publicHelloWorld 类(只有包私有的 HelloWorld.class),因此据我所知,它不应该编译。

回答by Qwerky

a java source file must have at least one public class and the file should be called class-name.java

一个 java 源文件必须至少有一个公共类,该文件应命名为 class-name.java

Incorrect, a top level class does not have to be declared public. The JLS states;

不正确,顶级类不必声明为公共。JLS 指出;

If a top level class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.

如果顶级类或接口类型未声明为 public,则只能从声明它的包内访问它。

See http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#104285section 6.6.1.

请参阅http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#104285第 6.6.1 节。

回答by Marek Sebera

You can place non-public class in a file, and it's not a bug but feature.

您可以将非公共类放在文件中,这不是错误而是功能。

Your problem is on level of packaging, not compile. Because you can compile this file with non-public class, but you can't call it from outside, so it's not working as application base class

您的问题在于打包级别,而不是编译级别。因为你可以用非公共类编译这个文件,但是你不能从外部调用它,所以它不能作为应用程序基类

Like this:

像这样:

// [+] single file: SomeWrapper.java 

public class SomeWrapper {
    ArrayList<_PrivateDataType> pdt;
}
// [-] single file: SomeWrapper.java 

// [+] single file: _PrivateDataType.java 
class _PrivateDataType {
    // members, functions, whatever goes here
}

// [-] single file: _PrivateDataType.java 

回答by Mathias Schwarz

A main method is just like any other method. The only difference is that it maybe invoked from the command line with the javacommand. Even if the main method is not visible from the command line, the class can still be used like any other Java class, and your main method may be invoked by another class in the same package. Therefore i makes sense that it compiles.

main 方法就像任何其他方法一样。唯一的区别是它可以通过命令从命令行调用java。即使 main 方法在命令行中不可见,该类仍然可以像任何其他 Java 类一样使用,并且您的 main 方法可能会被同一包中的另一个类调用。因此,我认为它可以编译。

In Java main function are not special in any sense. There just exists a terminal command that is able to invoke static methods called main...

在 Java 中,main 函数在任何意义上都没有什么特别之处。只存在一个终端命令,它能够调用称为 main 的静态方法...

回答by xong

that′s nothing to wonder about. I suppose this behavior is similar to the one of some C/C++-compiler.

这没什么好奇怪的。我想这种行为类似于某些 C/C++ 编译器的行为。

Code like "void main() { /.../ }" will be compiled correctly by those compilers, although it is not standards-compliant code. Simply said, the compiler exchanges the "void" with "int".

像“void main() { / .../ }”这样的代码将被这些编译器正确编译,尽管它不是符合标准的代码。简单地说,编译器将“void”与“int”交换。

I think a similar behavior is implemented by the java compiler.

我认为 java 编译器实现了类似的行为。

Regards, xong

问候, xong

回答by java_mouse

There are valid usages for a non public classes. So the compiler does not give error when you try to compile the file.

非公共类有有效的用法。因此,当您尝试编译文件时,编译器不会出错。

回答by Crollster

When you do not specify the access modifier of the class (or its field or method), it is assigned "default" access. This means it is only accessible from within the same package (in this case, the default package).

当您不指定类(或其字段或方法)的访问修饰符时,它会被分配“默认”访问权限。这意味着它只能从同一个包(在这种情况下,默认包)中访问。

The website Javabeginner.comhas an article on the subject - you should become familiar with access modifiers in Java, either from this site, or others.

网站Javabeginner.com有一篇关于这个主题的文章 - 您应该熟悉 Java 中的访问修饰符,无论是从这个网站还是其他网站。