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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With