Can we overload main() method in java:
Yes we can !!
But only public static void main(String[] args) will be used when your class is launched by JVM , as :-
But it will always print main(String[] args)
public class Test {
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)");
}
}
We can call the main() method yourself from code, of course - at which point the normal overloading rules will be applied.
Note that you can use a varargs signature, as that's equivalent from a JVM standpoint: -
public static void main(String... args)
Can we override the main method in java
No!
main is a static method, and is thus not polymorphic. You may hide it be defining another static main method in a subclass, though.
Yes we can !!
But only public static void main(String[] args) will be used when your class is launched by JVM , as :-
But it will always print main(String[] args)
public class Test {
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)");
}
}
We can call the main() method yourself from code, of course - at which point the normal overloading rules will be applied.
Note that you can use a varargs signature, as that's equivalent from a JVM standpoint: -
public static void main(String... args)
Can we override the main method in java
No!
main is a static method, and is thus not polymorphic. You may hide it be defining another static main method in a subclass, though.