Is this possible to implement code with two main function in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is this possible to implement code with two main function in Java?

If this is possible to compile a Java code with two main function then please understand me with an example.

30th Jul 2017, 8:01 AM
Avijit Mondal
Avijit Mondal - avatar
1 Answer
0
I don't believe so. Not in the same class. The main function provides a starting point for exacution and so too have two would cause ambiguity. Further more the main function cannot be overloaded as the OS will always pass arguments in the form of a single String array. The only way I can think of is to put the main function in a different class file. This would mean that java would call the main function of the file you exacuted. However, if you wish to distribute the code as a Jar this likely won't work in that format.
30th Jul 2017, 8:19 AM
josh mizzi
josh mizzi - avatar