我可以在 Java 项目中拥有多个包含 main() 方法的类吗?

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

Can I have more than one class containing the main() method in a Java project?

javaeclipsemain

提问by AndreaNobili

I have a doubt about Java.

我对Java有疑问。

In a Java project (handled by Eclipse for example), can I have more classes that contain the main() method and consequently can I chose to execute one class or another class?

在 Java 项目中(例如由 Eclipse 处理),我是否可以有更多包含 main() 方法的类,因此我可以选择执行一个类还是另一个类?

Tnx

Tnx

Andrea

安德烈亚

采纳答案by Som

Yes, you can have more classes that contain the main() method, but at least one class which contain main() should be public so that JMV will start that class as Main thread

是的,您可以拥有更多包含 main() 方法的类,但至少一个包含 main() 的类应该是公共的,以便 JMV 将该类作为主线程启动

  • as the code written by aUserHimselfrepresent
  • 正如aUserHimself所写的代码所代表的

回答by Prabhakaran Ramaswamy

Yes you can have more classes that contain public static void main(String[] args). And you can chose to execute one class or another class. However, you can't have more than one main method within same class.

是的,您可以拥有更多包含public static void main(String[] args). 您可以选择执行一个类或另一个类。但是,同一类中不能有多个主要方法。

回答by TheKojuEffect

You can have as many Classes as you want as long as each class have single mainmethod.

只要每个类都有一个main方法,您就可以拥有任意数量的类。

You'll have to be opening a Specific Class in Eclipse if you wanna run mainin that class or you can choose previously run classes from Eclipse RunMenuitem.

如果您想main在该类中运行,您必须在 Eclipse 中打开一个特定类,或者您可以从 Eclipse RunMenuitem 中选择以前运行的类。

mainmeans public static void main(String[] args)which is entry point in java programs.

main表示public static void main(String[] args)这是java程序的入口点。

回答by aUserHimself

Yes, you can have as many public static void main(String args[])methods as classes. You can also have more of them in the same file. For example, inside Class2.javayou can have:

是的,您可以拥有public static void main(String args[])与类一样多的方法。您还可以在同一文件中包含更多内容。例如,在里面Class2.java你可以有:

class Class1 {
    public static void main(String args[]) {
    }
}
public class Class2 {
    public static void main(String args[]) {
    }
}

回答by Dexter

Let me sum up the points regarding main method in JAVA (which is confusing at the beginning).

让我总结一下JAVA中关于main方法的要点(一开始很混乱)。

1. can we have more than one main() method in a class?Ans: Yes.You can have more than one method with the name main but different signature. These methods will be overloaded. BUTthe main method with following sigature will be treated as app entry point.

1. 一个类中可以有多个 main() 方法吗?答:是的。您可以拥有多个名称为 main 但签名不同的方法。这些方法将被重载。但是带有以下签名的 main 方法将被视为应用程序入口点。

public static void main(String args[]) which is same as public static void main(String... args)or public static void main(String[] args)

public static void main(String args[]) 与public static void main(String... args)public static void main(String[] args)

2. can we have more than one main method in a java program?Ans: Yes. We can have different classes having the main methods.

2. 一个java程序中可以有多个main方法吗?答:是的。我们可以有具有主要方法的不同类。

Then which one will be treated as app entry point?

那么哪一个将被视为应用程序入口点?

While running a program with such classes, the user will be asked to choose among the classes to act as entry point.

在运行具有此类类的程序时,将要求用户在这些类中进行选择以作为入口点。