Can there exist two main methods in a Java program?

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

提交回复
热议问题