1.

Overload main() method in Java

Answer»

It is quite possible to overload the main() method in Java as it is not an extra-terrestrial method. The main() method is like any other method and can be overloaded too like other methods.

As public static void main(STRING [] args) serves as the method signature of the main method, JVM calls its first. public static void main(String [] args) acts as the entry point for the Java program.

We can overload the main method in Java. SINCE the program doesn’t execute the overloaded main method when the program executes, we need to call the overloaded main method from the ACTUAL main method which has the method signature.

Let us see an EXAMPLE of overloading of the main method in Java

public class Example {      public static void main(String x)   {       System.out.println(x+" World");   }   public static void main(String a, int b)   {       System.out.println(a+","+b);   }   public static void main(String []args)   {     System.out.println("Hello from public static void main(String []args)!");      main("Hello");      main("Hello",2);   } }

The output is as FOLLOWS:

$javac Example.java $java -Xmx128M -Xms16M Example Hello from public static void main(String []args)! Hello World Hello,2


Discussion

No Comment Found