如果 main 方法在 java 文件的“非公共类”中怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12524322/
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
What if main method is inside "non public class" of java file?
提问by Ahamed
I have a java file containing more than one class, out of which one is public. If main method is inside a non-public class. I can't run that java file. Why is that? and there is no compilation error as well. If so, how can I use that main method?
我有一个包含多个类的 java 文件,其中一个是公共的。如果 main 方法在非公共类中。我无法运行该 java 文件。这是为什么?并且也没有编译错误。如果是这样,我该如何使用该主要方法?
采纳答案by Galaxin
You can certainly override main method and it does not violate any compiler rules and hence you will not have any compiler errors.
您当然可以覆盖 main 方法,它不会违反任何编译器规则,因此您不会有任何编译器错误。
You check that inspite of the fact that you have more than one class a file that is declared as public is the name of the file you are trying to execute.
尽管您有多个类,但您检查声明为 public 的文件是您尝试执行的文件的名称。
This is a convention that the file should be named after the same class which is public in that code.
这是一个约定,文件应该以该代码中公开的相同类命名。
Hence when you try to execute that class it does not have a main method from which it starts execution.So if you want to execute the main method in the non public class the only way to this is call that main from a main method of the public class.
因此,当您尝试执行该类时,它没有从中开始执行的 main 方法。因此,如果您想在非公共类中执行 main 方法,唯一的方法是从公开课。
回答by charles_ma
Actually you can execute the main method in a non-public class. if you put this class
实际上,您可以在非公共类中执行 main 方法。如果你把这个类
class A {
public static void main(String... args) {
System.out.println("This is not a public class!");
}
}
in a file named NonPubClass.java. You can compile this file using javac command but you will not get a NonPubClass.class, you will get a A.class instead. Use java a to invoke that class and you will see the printed string --- This is not a public class!
在一个名为 NonPubClass.java 的文件中。您可以使用 javac 命令编译此文件,但您不会得到 NonPubClass.class,而是会得到 A.class。使用 java a 调用该类,您将看到打印的字符串 --- 这不是公共类!
回答by Mohammad Faisal
Have a look at this code:
Super.java
看看这个代码:
Super.java
public class Super{ }
class Sub{
public static void main(String[] s){
System.out.println("Hello");
}
}
In order to print Hello
you can compile and run the program as:
为了打印,Hello
您可以编译并运行该程序:
How this works?
The compiler generates separate .class
file for every class in your program. So, instead of calling the main()
of non-public class from the public class's main()
you can print the output as shown above.
这是如何工作的?
编译器.class
为程序中的每个类生成单独的文件。因此,您可以打印如上所示的输出,而不是main()
从公共类调用非公共类main()
。
Note:As the convention says, you must put a public class
in separate file <class_name>.java
. And do not put more than one class in a single file (except if they are inner class) because if you would like to import them or use them with other classes then it will cause problem.
注意:正如约定所说,您必须将一个public class
放在单独的文件中<class_name>.java
。并且不要在一个文件中放置多个类(除非它们是内部类),因为如果您想导入它们或将它们与其他类一起使用,则会导致问题。
回答by user2440665
there is something i would like to add although everybody here believes that a public is necessary for the main in a class and that it won't work without main
我想补充一些东西,尽管这里的每个人都认为 public 是类中的 main 所必需的,并且没有 main 它将无法工作
you can have as many mains in a class as you desire, and you can have them without a public access modifier. but be careful, only that class which is named after the file can be public what i mean is if you name your file a.java, then only the class with name acan be public, none other can have this facility
您可以根据需要在一个类中拥有任意数量的电源,并且您可以在没有公共访问修饰符的情况下拥有它们。但要小心,只有以文件命名的类才能公开我的意思是,如果您将文件命名为 a.java,那么只有名称为a的类可以公开,其他人都不能拥有此功能
here is a code to show this : as you can see the name of the file is helping.java
这是显示此内容的代码:如您所见,文件名正在帮助.java
//:initialization/helping.java
class b{
public static void main(){
System.out.println("hello its b");
}
}
class helping {
static void f(float i, Character... c) {
System.out.println("first");
}
static void f(char a, Character... args) {
System.out.println("second");
}
public static void main(String[] args) {
f(1,'a');
f('a','b');
c.main();
}
}
class c{
public static void main(){
System.out.println("hello its b");
}
}
//:~
/*
* output:
* first
* second
* hello its b
* */
回答by Priyank Doshi
Simple Answer. You can't
. You need to have main method in a public class
and its signature should be public static void main(String... args)
简单的回答。You can't
. 你需要在 a 中有 main 方法public class
,它的签名应该是public static void main(String... args)
there is no compilation error
没有编译错误
Why there would be? You are doing nothing wrong as far as compilation rulesare concerned.
Only thing is that your non-public-class-main-method
won't work as an entry point of your code.
为什么会有?就编译规则而言,您没有做错任何事情。唯一的问题是您non-public-class-main-method
不能作为代码的入口点。
回答by roshan
回答by Rakesh Pathak
It's not a compile time error as u mentioned that top level type declaration shouldn't be protected, static or private.
这不是编译时错误,因为您提到顶级类型声明不应受到保护、静态或私有。
If u go through the link http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.6well that u have shared ,then it's quite clear there that a top-level type declaration refers to only "top level Class and Interface type declarations" and these should not be protected, static or private at top level declarations, but we can use protected, static or private for any methods or variable declaration inside them.
如果您通过链接http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.6以及您共享的链接,那么很明显那里有一个顶级-级别类型声明仅指“顶级类和接口类型声明”,这些在顶级声明中不应该是受保护的、静态的或私有的,但是我们可以对其中的任何方法或变量声明使用受保护的、静态的或私有的。
With respect to above code, there is nothing wrong in declaration, and the code will compile and run successfully as all outer top level class are default and there is no violation.
上面的代码,声明没有任何问题,代码编译运行成功,所有外部顶级类都是默认的,没有违规。
The answer to the question asked at top is exactly as mentioned by few experts at top, that
楼上问题的答案和楼上几位专家说的完全一样,就是
"for sure we can have a file with main method inside non-public class and it will compile as well as run successfully, but make sure that at the time of running the program we have to pass the class name of "main method" to the java interpreter instead of the class which is public."
“当然我们可以在非公共类中有一个带有 main 方法的文件,它会编译并成功运行,但请确保在运行程序时我们必须将“main 方法”的类名传递给Java 解释器而不是公共类。”
If we have 2 classes A(public) and B(non-public containing main method) , then the file will compile with "javac A.java" but while running the code we need to pass the command as "java B" .
如果我们有 2 个类 A(public) 和 B(non-public contains main method) ,那么文件将使用 "javac A.java" 编译,但在运行代码时,我们需要将命令作为 "java B" 传递。