Monday, 12 February 2018

Can you Overload or Override main method in Java?

One of the common doubt among Java beginners while learning overloading and overriding is, whether it's possible to overload main in Java ? Can you override the main method in Java? How will JVM find if you change the signature of the main method as part of method overloading? etc. These are a good question and shows curiosity and application of knowledge of students, so if you are the tutor you must answer these questions. The short answer to, can we overload the main method in Java is Yes, you can overloading, nothing stops from overloading, but JVM will always call the original main method, it will never call your overloaded main method.

In order to answer this question understanding of overloading and overriding is necessary. You should know that overloading takes place at compile time and overriding takes place at runtime. You should also be familiar with rules of method overloading and overriding in Java.

Though main() is a special method because its the entry point of your Java application, it follows same rule of method overriding and overloading like any other method.

Overloading main method in Java


Here is how you can overload main method in Java. This is just one way, you can create as many version of main as you want, but you must make sure that method signature of each main is different. You can change method signature by changing type of argument, number of arguments or order of arguments. Best practice to overload a method is by changing number of arguments, or type of arguments. Overloading methods just by changing order of arguments may create confusion and increase chance of calling wrong method. Its worth remember that, In Java, its not possible to overload the method by just changing return type.

Oracle Java Tutorial and Materials, Oracle Java Guides, Oracle Java Learning

/**
 * Java Program to show that you can overload main method in Java
 * but you cannot override main method.
 *
 * @author Javin Paul
 */
public class Helloworld {

    /**
     * Standard main method, JVM will only call this method
     * even if you provided multiple overloaded version.
     *
     */
    public static void main(String[] args) {
        System.out.println("Inside main(String[] args) method ....");
     
    }
 
    /**
     * An overloaded main method which accepts Integer[] instead of
     * String[] as argument.
     * @param args
     */
    public static void main(Integer[] args){
        System.out.println("Inside main(Integer[] args) method ....");
    }
 
 
    /**
     * Another overloaded main method which accepts Double[] instead of
     * String[] as argument.
     * @param args
     */
    public static void main(Double[] args){
        System.out.println("Inside main(Double[] args) method ....");
    }

}

Output
Inside main(String[] args) method ....

In this example, you can see that we have two main methods, one accepts String array as argument and other accepts Integer array as argument, but you can see that when you run our program from command line, only the main method with string array as argument is called.

There was no error, no ambiguity, JVM will always call this main method, no matter how many overloaded main method you will put on this class. Then questions comes how do you call your overloaded main? Well, you can call it just like any other method.

Simply calling main(new int[]{1, 2, 3}) from original main method will invoke your overloaded main with integer array as argument. Since there is no compilation error, it proves that you can overload main method in Java.

Regarding Overriding we have already proven that static method cannot be overridden, then can only be hided.

Oracle Java Tutorial and Materials, Oracle Java Guides, Oracle Java Learning

That's all about overloading and overriding main method in Java. Now you know that its possible to overload main in Java but its not possible to override it, simply because its a static method. Execution of Java program has no impact on overloading main because JVM always call the original main method and if it doesn't found in class then it throws java.lang.NoSuchMethodError: main Exception in thread "main" error at runtime.

Related Posts