can we overload main method?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

can we overload main method??

13th Aug 2016, 5:23 PM
divya
divya - avatar
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.
13th Aug 2016, 6:50 PM
James
James - avatar
+ 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.
14th Aug 2016, 1:48 AM
James
James - avatar
0
What you mean is by extending the main class. Isn't it?
13th Aug 2016, 10:49 PM
WPimpong
WPimpong - avatar
0
what I know is you can overload d main method by changing d argument..but wtever he told is ryt
14th Aug 2016, 1:48 AM
divya
divya - avatar
0
Thank you @Jame.
14th Aug 2016, 6:38 AM
WPimpong
WPimpong - avatar
0
Yes we can extend the class by using this class hloo1 extends class hloo
15th Aug 2016, 4:24 PM
Paras
0
give me your number
3rd Dec 2016, 11:25 AM
Khilan Mistry
Khilan Mistry - avatar
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
6th Jan 2017, 2:23 PM
abhishek maurya
abhishek maurya - avatar
- 1
thankew so much.. understood very well
14th Aug 2016, 2:10 AM
divya
divya - avatar
- 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
14th Aug 2016, 6:49 AM
Gowtham krvz kv
Gowtham krvz kv - avatar