Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (6.5k points)
edited by
Could we overload the main() method?

1 Answer

0 votes
by (11.3k points)

Yes, you absolutely can. Similar to normal method overloading, the main method can also be overloaded as many times as you want. Keep in mind though, the main method is the driver code for any program and is slightly different from a normal method. The way you can see the implementation of the above is by changing the number of arguments or datatype of arguments you add in the command line when you use the 'java' command for execution. I'll explain with the following:

/* You have two main methods in the following way */

class hello{

public static void main(int a, int b){ //code }

public static void main(int c) { //code }

}

So, we first compile the code with 'javac hello.java' and then we use:

java hello 1 2Used to call for the first main method with 2 arguments

java hello 3 - Used to call for the second main method with 1 argument

I hope this clarifies your doubts. 

Related questions

Browse Categories

...