Can there exist two main methods in a Java program?

后端 未结 16 629
后悔当初
后悔当初 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:16

    The answer is Yes, but you should consider the following 3 points.

    1. No two main method parameter should be the same

      Eg.

      • public static void main(int i)
      • public static void main(int i, int j)
      • public static void main(double j)
      • public static void main(String[] args)
    2. Java’s actual main method is the one with (String[] args), So the Actual execution starts from public static void main(String[] args), so the main method with (String[] args) is must in a class unless if it is not a child class.

    3. In order for other main methods to execute you need to call them from inside the (String[] args) main method.

    Here is a detailed video about the same: https://www.youtube.com/watch?v=Qlhslsluhg4&feature=youtu.be

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

    the possibility of two main(String[] args) methods within the same scope create confusion for the JVM. It fails to use them as overloaded methods. So the signatures in terms of parameters) must be different.

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

    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.

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

    Yes! Any class in Java can have multiple main methods. It's called Overloading (Overloaded methods are methods with same name but with different signatures) but there should only be one main method with parameters like this :- (String[] args) or (String args[])

    For example :-public class E {

    public static void main(String args){
        System.out.println("Print A");
    }
    public static void main(String [] args){
        System.out.println("Print B");
    }
    public static void main(int garbage){
        System.out.println("Print C");
    }
    public static void main(int i){
        System.out.println("Print D")
    }
    

    }

    The output of the above program will be "Print B" as only that main method contains the correct method signature identified by the Javac compiler and it compiles that only and leaves the rest.

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