How does java know what class to invoke the main() method of?

In summary, in C++, any global main() function will be run, while in C#, the Main() method of any class will be run. Having multiple Main() methods in multiple classes may cause a compilation error. In Java, when running a .jar file, the class's main() method that will be run can be specified in the manifest file or through command line options.
  • #1
Whovian
652
3
In C++, pretty much any global main() function gets run. In C#, amy class's Main() method gets run, and having multiple Main() methods in multiple classes should give a compilation error(?). But if we have a .jar, when running it, how do we know which class's main() method the .jar will run?
 
Technology news on Phys.org
  • #2
Hey Whovian.

Actually for C and C++, you have to provide an entrypoint in many cases for different compilers and the 'main' is not always assumed to be main (although it is set by default to do this).

As for your question in Java, a quick google search gave this:

http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
 

1. How does Java determine which class to invoke the main() method of?

Java determines which class to invoke the main() method of by looking for the class with a public static void main(String[] args) method. This method is the entry point for the Java program and is called by the Java Virtual Machine (JVM) when the program is executed.

2. Can multiple classes have a main() method in a Java program?

Yes, multiple classes can have a main() method in a Java program. However, only one of these classes can be designated as the entry point for the program and be executed by the JVM. The other main() methods can be used as regular methods within the program.

3. What happens if a Java program does not have a main() method?

If a Java program does not have a main() method, it will not be able to run. The JVM requires the main() method to be present in order to execute the program. Without it, the program will not be recognized as a valid Java program.

4. Can the main() method have parameters other than String array?

Yes, the main() method can have parameters other than a String array. However, the main() method must always have a String array as its parameter list. Any additional parameters must be specified after the String array parameter.

5. Can the main() method be overloaded in Java?

Yes, the main() method can be overloaded in Java. This means that multiple versions of the main() method can exist within a single class, as long as they have different parameter lists. However, only the main() method with the standard String array parameter will be recognized as the entry point by the JVM.

Similar threads

  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
363
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
9
Views
902
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top