Which of the following options cannot be the signature for the main method

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. 


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

Though main() is a special method because it's the entry point of your Java application, it follows the same rule of method overriding and overloading as any other method. In this article, you will learn how to overload the main method to see whether JVM calls the original, standard main with string argument or not.




Overloading the main method in Java

Here is how you can overload the main method in Java. This is just one way, you can create as many versions of the main as you want, but you must make sure that the method signature of each main is different. 


You can change the method signature by changing the type of argument, the number of arguments, or the order of arguments. 


The best practice to overload a method or constructor is by changing the number of arguments, or types of arguments. Overloading methods just by changing the order of arguments may create confusion and increase the chance of calling the wrong method. It's worth remembering that,


In Java, it's not possible to overload the method by just changing the return type.


Which of the following options cannot be the signature for the main method





Code Example of Overloading 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 an argument and the other accept Integer array as an argument, but you can see that when you run our program from the command line, only the main method with string array as an argument is called.

There was no error, no ambiguity, JVM will always call this main method, no matter how many overloaded main methods you will put on this class. Then questions come 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 the original main method will invoke your overloaded main with integer array as an argument. Since there is no compilation error, it proves that you can overload the main method in Java.

Regarding Overriding we have already proven that the static method cannot be overridden, they can only be hidden. See that post to learn this concept by following an example. In short, the main method can be overloaded but cannot be overridden in Java.

Which of the following options cannot be the signature for the main method



That's all about overloading and overriding the main method in Java. Now you know that it's possible to overload main in Java but it's not possible to override it, simply because it's a static method. Execution of Java program has no impact on overloading main because JVM always calls 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 tutorials, I suggest checking out the following tutorials from this blog :

  • 130+ Java Interview Questions from the last 5 years (questions)
  • Understanding the Helloworld Program in Java (explanation)
  • How to run a Java program from the command prompt? (example)
  • 10 things Every Java programmer should know about the main method (article)
  • Can you run a Java program without the main method? (answer)
  • What is the difference between PATH and CLASSPATH in Java? (answer)
  • What is a .class file in Java? How does it work? (answer)
  • 40 Binary Tree-Based Algorithms Questions (BST problems)
  • Top 5 Object Oriented Design Interview Questions (OOP questions)
  • How to make executable JAR in Eclipse? (steps)
  • 20+ System Design Questions for Coding interviews (design questions)
  • Difference between Overloading, Overriding, and Shadowing (answer)
  • How to run a Java program from the JAR file? (example)
  • 10 Best System Design Courses for Interviews (system design courses)
  • The Complete Java Developer RoadMap (guide)
  • Top 30 Linked List Interview Questions (linked list problems)

Thanks for reading this article so far. if you find this Java interview question useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 


P. S. - If you are new to the Java world and looking for a free online course to start learning Java then I highly recommend you check out this Java Tutorial for Complete Beginners(FREE) course by John Purcell on Udemy. It's completely free and you just need an Udemy account to join this course. 

Which of the following options can be the signature for the main method?

public static void main(String a[]) is the main entry point signature for a typical Java program. So you should get on with this method signature. Save this answer.

What is the method signature of the main method?

main(): It is a default signature which is predefined in the JVM. It is called by JVM to execute a program line by line and end the execution after completion of this method. We can also overload the main() method. String args[]: The main() method also accepts some data from the user.

Which of the following is not a valid statement class methods can access?

class methods cannot access instance variable or instance methods directly_they must use an object reference​

What is the correct way to declare the main method in Java?

Main Method Structure The declaration of the Java main method is: public static void main(String[] args) { //Your code goes here. }