+ 3
can we overload main method??
10 Answers
+ 3
Yes, But keep in mind the Normal main method always gets called first, regardless of inputs. You just have to call the overloaded mains from the original main method.
+ 3
No @ Wachirakorn. I'll give an example.
class Program{
public static void main(String[] args){
System.out.println("This one will always run first, because it has String[] args, which is what the JVM looks for");
main(args[0]);
}
public static void main(String args){
System.out.println("This main has different parameters so is overloaded, but won't be called by JVM because only String[] main gets called first, will only be called by the code in the accepted main method.");
System.out.println("This is the argument taken from console and given to us by the String[] main method" + args);
}
public static void main(String args1, String args2){
System.out.println("This method won't be called at all, even if two string arguments are passed to console. Only String[] gets called, and that main hasn't called this method in its body");
}
}
Basically, because of this limitation, overloading a main method is pointless because you can't get the JVM to call one of the other methods first, regardless of number of arguments, or types of arguments.
0
What you mean is by extending the main class. Isn't it?
0
what I know is you can overload d main method by changing d argument..but wtever he told is ryt
0
Thank you @Jame.
0
Yes we can extend the class by using this
class hloo1 extends class hloo
0
give me your number
0
yes but your care about argument it's means don't use same signature as main.
you can use method name as overload concept
- 1
thankew so much.. understood very well
- 3
I think it is possible to overload main method.
it is necessary that main method should be static..(that is the actual main method)
public static void main(String[] args)//this will be the main method..
create another method but without static keyword. it is possible to overload