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. we will learn this in little more detail later, now coming to next question, can you override the main method in Java? the answer is No because main is a static method and static method cannot be overridden in Java.

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. In this article, you will learn how to overload main method to see whether JVM calls the original, standard main with string argument or not.




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.

Can we overload main method in Java


/**
 * 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. See that post to learn this concept by following an example. In short, main method can be overloaded but cannot be overridden in Java.

Main method can be overloaded but cannot overridden in Java


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.

If you like this tutorial and looking for some more Java beginners tutorial, I suggest to check out following tutorials from this blog :
  • Understanding Helloworld Program in Java (explanation)
  • How to run Java program from command prompt? (example)
  • 10 things Every Java programmer should know about main method (article)
  • How to run Java program from JAR file? (example)
  • Can you run a Java program without main method? (answer)
  • How to make executable JAR in Eclipse? (steps)
  • What is difference between PATH and CLASSPATH in Java? (answer)
  • What is .class file in Java? How does it work? (answer)

Recommended Java Books for Beginners

  • Head First Java 2nd Edition by Kathy Sierra and team (check here)
  • Core Java Volume 1 9th Edition by Cay S. Horstmann (check here)
  • Java: A Beginner's Guide by Herbert Schildt (check here)

2 comments:

  1. Why do someone need to overload main? forget about overriding it? I have never seen a scenario, where you need to overload main() in Java.

    ReplyDelete
    Replies
    1. This situation may not arise, But Its the flexibility that java provides for overloading main.

      Delete