-
Overload main method
How to overload the main method? I know that main() can be overloaded but don't know how to invoke overload version.
public class A{
public static void main(String args[]){
System.out.println("Hi");
}
public static void main(int a){
System.out.println("Hi i m int");
}
}
main() is overload in the above code but how to invoke the overloaded version?
[b]Question asked by visitor ashok agnihotri[/b]
-
Re: Overload main method
-
Re: Overload main method
Hi
At first the JVM run the public static void main(String args[]) method. Then second the main method is called the any object and class.
-
Re: Overload main method
public class FirstExample{
public static void main(String a[]){
System.out.println("First");
main(1);
}
public static void main(int i ){
System.out.println("Second");
}
}
-
Re: Overload main method
I think generally to invoke the overloaded method we pass the type of argument to it.
here also if we pass string as argument it will invoke the first main method and if we pass int value as argument then senod main method will be invoked..
I am not sure about the answer
Please correct me if I am wrong.
Thanks in advance
-
Re: Overload main method
public class FirstExample{
public static void main(String a[]){
System.out.println("First");
FirstExample.main(100);
}
public static void main(int i ){
System.out.println("Second");
}
}
-
Re: Overload main method
i agree with the reply from Rajan. That is the way to overload main()
Even if u give any command line argument other than String type, JVM will consider it as String only..and obviously it starts the main() method with argument String.
Because the JVM will check for the method signature of main with String args...and starts execution from that main only...
-
Re: Overload main method
[QUOTE=gaurav0252;21499]public class FirstExample{
public static void main(String a[]){
System.out.println("First");
main(1);
}
public static void main(int i ){
System.out.println("Second");
}
}[/QUOTE]
...........Thanks i m also searching same;)
-
Re: Overload main method
Hii....
Definetly y not ...
Not only main method We can overload any method in a class except 'final' , nobody will stops us , but what is the use by overloading the main method.
if we overload the main method then its just acts as a normal method.