Can there exist two main methods in a Java program?

后端 未结 16 630
后悔当初
后悔当初 2020-12-08 02:21

Can there exist two main methods in a Java program?

Only by the difference in their arguments like:

public static void main(String[] args)

相关标签:
16条回答
  • 2020-12-08 03:01

    Case :1 > We have two main method but with "Main" AND "main2" so jvm only a=calling "main" method .

    2> Same method but diff params, still jvm calling "main(String[] args) " meyhod.

    3> Exactly same method but it gives copile time error as you can not have two same name method in a single class !!!

    Hope it will give you clear pic

    0 讨论(0)
  • 2020-12-08 03:06

    i have check in java version 1.6.0_32 multiple main method is working but there should one main method like public static void main(String []args) of type signature. Ex is here which i had tested.

    public class mainoverload
    {
    public static void main(String a)
    {
        System.out.println("\nIts "+a);
    }
    public static void main(String args[])
    {
        System.out.println("\nIts public static void main\n");
        mainoverload.main("Ankit");
        mainoverload.main(15,23);
        mainoverload.main(15);
    }
    public static void main(int a)
    {
        System.out.println("\nIts "+a);
    }
    public static void main(int a,int b)
    {
        System.out.println("\nIts "+a+" "+b);
    }
    }    
    
    0 讨论(0)
  • 2020-12-08 03:08

    There can be more than one main method in a single program. Others are Overloaded method. This overloaded method works fine under a single main method

    public class MainMultiple{
    
       public static void main(String args[]){
           main(122);
           main('f');
           main("hello java");
       }
    
       public static void main(int i){
           System.out.println("Overloaded main()"+i);
       }
    
       public static void main(char i){
           System.out.println("Overloaded main()"+i);
       }
    
       public static void main(String str){
           System.out.println("Overloaded main()"+str);
       }
    }
    
    0 讨论(0)
  • 2020-12-08 03:10

    Here you can see that there are 2 public static void main (String args[]) in a single file with the name Test.java (specifically didn't use the name of file as either of the 2 classes names) and the 2 classes are with the default access specifier.

    class Sum {
    
        int add(int a, int b) {
            return (a+b);   
        }
    
        public static void main (String args[]) {
            System.out.println(" using Sum class");
            Sum a = new Sum();
            System.out.println("Sum is :" + a.add(5, 10));
        }
    
        public static void main (int i) {
            System.out.println(" Using Sum class main function with integer argument");
            Sum a = new Sum();
            System.out.println("Sum is :" + a.add(20, 10));
        }
    }
    
    class DefClass {
    
        public static void main (String args[]) {
            System.out.println(" using DefClass");
            Sum a = new Sum();
            System.out.println("Sum is :" + a.add(5, 10));
            Sum.main(null);
            Sum.main(1);
        }
    }
    

    When we compile the code Test.java it will generate 2 .class files (viz Sum.class and DefClass.class) and if we run Test.java we cannot run it as it won't find any main class with the name Test. Instead if we do java Sum or java DefClass both will give different outputs using different main(). To use the main method of Sum class we can use the class name Sum.main(null) or Sum.main(1)//Passing integer value in the DefClass main().

    In a class scope we can have only one public static void main (String args[]) per class since a static method of a class belongs to a class and not to its objects and is called using its class name. Even if we create multiple objects and call the same static methods using them then the instance of the static method to which these call will refer will be the same.

    We can also do the overloading of the main method by passing different set of arguments in the main. The Similar example is provided in the above code but by default the control flow will start with the public static void main (String args[]) of the class file which we have invoked using java classname. To invoke the main method with other set of arguments we have to explicitly call it from other classes.

    0 讨论(0)
  • 2020-12-08 03:10

    The signature of main method must be

    public static void main(String[] args) 
    
    • The parameter's name can be any valid name
    • The positions of static and public keywords can be interchanged
    • The String array can use also the varargs syntax

    A class can define multiple methods with the name main. The signature of these methods does not match the signature of the main method. These other methods with different signatures are not considered the "main" method.

    0 讨论(0)
  • 2020-12-08 03:16

    As long as method parameters (number (or) type) are different, yes they can. It is called overloading.

    Overloaded methods are differentiated by the number and the type of the arguments passed into the method

    public static void main(String[] args)
    

    only main method with single String[] (or) String... as param will be considered as entry point for the program.

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