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

I have doubts about Java.

In a Java project (for example, using Eclipse), is it possible to have more classes containing the main () method, and therefore, is it possible to select one class or another class?

Tpx

Andrea

+6
source share
5 answers

Yes, you can have more classes that contain the main () method, but at least one class that contains main () must be publicly available so that JMV runs this class as the main thread

  • since the code written by aUserHimself represents
+2
source

You can have as many classes as you want if each class has one main method.

You will need to open a specific class in Eclipse if you want to run main in this class or you can select previously launched classes from Eclipse Run Menuitem.

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

+7
source

Yes, you can have as many public static void main(String args[]) methods as there are classes. They can also be found in one file. For example, inside Class2.java you can:

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

Let me summarize the main method in JAVA (which is confusing at the beginning).

1. can we have more than one main () method in a class? Ans: Yes. You can have several methods named main, but with a different signature. These methods will be overloaded. BUT the main method with the next cigar will be considered as the entry point of the application.

public static void main(String args[] ), which is similar to public static void main(String... args) or public static void main(String[] args)

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

Then which one will be considered as the entry point to the application?

When starting a program with such classes, the user will be asked to choose among the classes so that they act as an entry point.

+2
source

Yes, you can have more classes containing public static void main(String[] args) . And you can choose one class or class. However, you cannot have more than one main method within the same class.

+1
source

Source: https://habr.com/ru/post/954588/


All Articles