Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can there exist two main methods in a Java program?

Can two main methods exist in a Java program?

Only by the difference in their arguments like:

public static void main(String[] args)

and second can be

public static void main(StringSecond[] args)

If it is possible, which Method will be used as the entry point? How to identify this?

like image 474
joey rohan Avatar asked Nov 15 '12 14:11

joey rohan


5 Answers

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.

like image 91
Swarish Avatar answered Oct 17 '22 10:10

Swarish


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.

like image 21
kosa Avatar answered Oct 17 '22 11:10

kosa


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);
   }
}
like image 8
Ram Sharma Avatar answered Oct 17 '22 12:10

Ram Sharma


Only public static void main(String[] args) counts. This is the only signature considered to be the true main() (as the program entry point, I mean).

like image 5
dashrb Avatar answered Oct 17 '22 11:10

dashrb


That would be compilable code, as long as StringSecond was a class. However, if by "main method" you mean a second entry point into the program, then the answer to your question is still no. Only the first option (public static void main(String[] args)) can be the entry point into your program.

Note, however, that if you were to place a second main(String[]) method in a different class (but in the same project) you could have multiple possible entry points into the project which you could then choose from. But this cannot conflict with the principles of overriding or overloading.

Also note that one source of confusion in this area, especially for introductory programmers, is that public static void main(String[] args) and public static void main(String ... args) are both used as entry points and are treated as having the same method signature.

like image 5
asteri Avatar answered Oct 17 '22 11:10

asteri