Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Main Functions

Tags:

java

main

c#

I'm a bit new at this so bear with me. I'm currently learning C# and Java and one of their similarities is that the main function needs to be encapsulated within a class. For example

public class HelloWorld {     public static void main(String[] args) {         // Some Code     } } 

Now I understand that main is often the "entry point" when you run the program. So basically, your program will start executing wherever the main function is. But I believe in both languages you can have multiple main functions within multiple classes. So when I compile a project with multiple main functions, where is the "entry point"? How does the compiler know where to start?

like image 396
Ben Avatar asked Jul 18 '12 22:07

Ben


2 Answers

In Java, the computer determines the "entry point" when you actually execute the program, not when you compile. For example, from the command-line

java MyClass 

searches for main() in MyClass. All other main() functions are ignored.

If you are using an IDE, then you can set which class contains the main() function that you want to use.

like image 76
Code-Apprentice Avatar answered Sep 28 '22 12:09

Code-Apprentice


In .NET, you can define which class contains the Main method you want to use when you're compiling.

http://msdn.microsoft.com/en-us/library/x3eht538.aspx

In Java, if you're bundling to a jar, you can define your entry point in the jar's manifest.

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

like image 24
MStodd Avatar answered Sep 28 '22 13:09

MStodd