When it allowed to use any no.of main methods in java, how to predict the order of execution? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

When it allowed to use any no.of main methods in java, how to predict the order of execution?

Seems Silly, but Confused. Is compiler executes any no.of main methods(from different class) in single program? I wonder it is possible. But only top level public class allowed. Then how it be predict the order of execution when every main method id automatically executing? class program1 { public static void main(String[] a) { System.out.print("p1"); } } clasd program2 { public static void main(String a[]) { System.out.println("p2"); } } Output:?(compiler dependent)? Edit: The above one giving as expected but actually am looking for this. https://code.sololearn.com/chFbAY52rHF5/?ref=app Look at this code?

14th Dec 2019, 9:33 AM
Jayakrishna 🇮🇳
13 Answers
+ 4
Jaya krishna Yes I checked it by running it on the code playground and I completely forgot that we do not have file names here. Also running a code to check whether it matches your explanation is a good practice according to me because I never want to give a wrong explanation for any question. Also 1 single file can have multiple classes but there is a single entry point per file. So the other main method in the same file but in other class is not executed I guess. Someone correct me if I'm wrong.
14th Dec 2019, 10:37 AM
Avinesh
Avinesh - avatar
+ 3
There can be only one main method per class with the method signature - public static void main(String[] args) Edit: 1) Name of the parameter can be anything. 2) Also you can use varargs instead of [ ]. 3) Placement of public and static can be replaced with each other. You can have multiple overloaded main methods within a class. As you know since it cannot be overridden, there is only a single entry point for the program. EDIT: Also your code will work fine and the class declared which is same as your file name would be executed first.
14th Dec 2019, 10:01 AM
Avinesh
Avinesh - avatar
+ 3
Jaya krishna did you run it on an IDE on your system? Is it still giving the same behaviour?
14th Dec 2019, 12:14 PM
Avinesh
Avinesh - avatar
+ 3
Jaya krishna As I already mentioned- "One single file can have multiple classes but there is a single entry point per file. So the other main method in the same file but in other class is not executed I guess." If the above is true then only the class name matching the file name will let it's main method execute. So the entry point is found and since a java file cannot have multiple entry points, the remaining main methods in other classes are just ignored. I might be wrong. So let us wait for someone else to answer this question.
14th Dec 2019, 12:55 PM
Avinesh
Avinesh - avatar
+ 3
Yes. I thought the same but the program above ( i mentioned)surprised me with output. Put me to a beginner level of confusion state. Denise Roßberg Thank you for your answer... Avinesh thanks you too...
14th Dec 2019, 8:15 PM
Jayakrishna 🇮🇳
+ 2
you can use any number of main()s in java but the one with String[] and args as its parameter will be referred to as the original main() and perceived by the compiler as one REAL main(). Be careful so as to never use String[] and args as parameters to two of the main()s in same program
16th Dec 2019, 1:47 AM
Aditya
Aditya - avatar
+ 1
Avinesh So what is your output? p1 p2.? I expected the same. But not happening.. That's why i confused. And there is only 1 main method per class. And no overloading, no overriding presented...
14th Dec 2019, 10:15 AM
Jayakrishna 🇮🇳
+ 1
Avinesh Yes. Its true that execution starts from matching class name. And other main methods in same file here are executing automatically.. But order is as expected in different files. I expected only 1 main method is automatic but all are automatic. And order of execution i expected first one first but varying program to program... Look at this code below...
14th Dec 2019, 10:49 AM
Jayakrishna 🇮🇳
+ 1
The above one giving as expected but actually am looking for this. https://code.sololearn.com/chFbAY52rHF5/?ref=app Look at this code?
14th Dec 2019, 10:49 AM
Jayakrishna 🇮🇳
+ 1
Avinesh No. I dont have any system now. So predicting the behavior is limited to this compiler only.. And also to make sure it, posted question asking is there any logic which I'm missing?
14th Dec 2019, 12:48 PM
Jayakrishna 🇮🇳
+ 1
Maybe this is helpful: https://stackoverflow.com/questions/46797988/java-multiple-classes-and-multiple-main-methods-execute-all-main-methods I made a test in eclipse. It does not execute each main methods. class1, class2, class3 open class1 -> execute main of class1 open class2 -> execute main of class2
14th Dec 2019, 6:33 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Denise Roßberg So it means that, the behavior, and order of execution is limited to solo learn only...?
14th Dec 2019, 6:41 PM
Jayakrishna 🇮🇳
+ 1
Jaya krishna I think yes. Here is an example for netbeans: https://www.thoughtco.com/using-multiple-main-classes-2034250 Now I know that I can do this also in eclipse. Multiple main classes -> define one class as "real" main class -> executes this main method I found also an online compiler: https://www.compilejava.net First class which contains main method -> executes only this main method I am really wondering about sololearns way to deal with multiple main classes
14th Dec 2019, 8:03 PM
Denise Roßberg
Denise Roßberg - avatar