Multiple main() methods in java

后端 未结 10 585
忘了有多久
忘了有多久 2020-12-28 08:53

I was wondering what the effect of creating extra main methods would do to your code.

For example,

public class TestClass {
    public static void ma         


        
相关标签:
10条回答
  • 2020-12-28 09:21

    It will cause no errors. Just because you initialize an object, doesn't mean the main method gets executed. Java will only initially call the main method of the class passed to it, like

    >java TestClass

    However, doing something like:

    public class TestClass
    {
     public static void main (String[] args)
     {
      TestClass foo = new TestClass();
      foo.main(args);
     }
    }
    

    Or

    public class TestClass
    {
     public TestClass()
     {
       //This gets executed when you create an instance of TestClass
       main(null);
     }
    
     public static void main (String[] args)
     {
      TestClass foo = new TestClass();
     }
    }
    

    That would cause a StackOverflowError, because you are explicitly calling TestClass's main method, which will then call the main method again, and again, and again, and....

    When in doubt, just test it out :-)

    0 讨论(0)
  • 2020-12-28 09:28

    The main method is static, which means it belongs to the class rather than the object. So the object won't have another main method inside it at all.

    You could call the main method on instances of the object, but if you do that it's literally just another way of calling TestClass.main() (and it's frowned upon by many, including me, to call a static method on an instance of an object anyway.)

    If you're referring to multiple main methods in the same program, then this isn't a problem either. The main class is simply specified and its main method is executed to start the program (in the case of a jar file this is the main-class attribute in the manifest file.)

    0 讨论(0)
  • 2020-12-28 09:30

    After searching for a Java Class with multiple main() methods or in plain words, overloaded main() methods, I came up with an example of my own. Please have a look

    public class MultipleMain{
    
      public static void main(String args[]){
          main(1);
          main('c');
          main("MyString");
      }
    
      public static void main(int i){
         System.out.println("Inside Overloaded main()");
      }
    
      public static void main(char i){
         System.out.println("Inside Overloaded main()");
      }
      public static void main(String str){
         System.out.println("Inside Overloaded main()");
      }
    

    }

    I tested this Java Code on JDK 1.7 and works like a charm !

    You need "public static void main(String args[])" to start with and then you can call overloaded main methods inside this main and it should work for sure.

    Any comments and suggestion are highly appreciated. I am just a novice Java Developer willing to develop my Java skills.

    Thanks, PK

    0 讨论(0)
  • 2020-12-28 09:31

    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.

    0 讨论(0)
提交回复
热议问题