Multiple main() methods in java

后端 未结 10 644
忘了有多久
忘了有多久 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: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

提交回复
热议问题