I was wondering what the effect of creating extra main methods would do to your code.
For example,
public class TestClass {
public static void main (String[] args){
TestClass foo = new TestClass();
}
}
After the program initially starts up, foo will be created and it would have another public main method inside it. Will that cause any errors?
You can have only one main method in one class, But you can call one main method to the another explicitly
No, you can have any number of main-methods in a project. Since you specify which one you want to use when you launch the program it doesn't cause any conflicts.
This is perfectly fine. Having multiple
main
methods doesn't cause any problems. When you first start a Java program, execution begins in some function calledmain
in a class specified by the user or by the .jar file. Once the program has started running, all the other functions calledmain
are essentially ignored or treated like other functions.It won't have an additional
main
-method, asmain
isstatic
. So it's once per class.If you have multiple
main
-methods in your project, you will specify which one to launch when starting your application.