Can we overload main method
Answer: Sure you could overload main method without any issues but only public static void main(String[] args) will be used when your class is launched by the JVM. For example:
package com.javahonk; public class MainMethodTest { public static void main(String[] args) { System.out.println("main(String[] args)"); } public static void main(String arg1) { System.out.println("main(String arg1)"); } public static void main(String arg1, String arg2) { System.out.println("main(String arg1, String arg2)"); } }
Above will always print:
You can call the main() method yourself from code and execution point the normal overloading rules will be applied.
Note: You can use a varargs signature, as that’s equivalent from a JVM standpoint shown below:
public static void main(String… args) is equivalent to public static void main(String[] args) from Java 1.5 onward.