Can we overload main() method?

Can we overload main() method?

Yes, by method overloading. You can have any number of main methods in a class by method overloading. Let's see the simple example:

class test{

public static void main(int x){

System.out.println(x);

}

public static void main(String args[]){

System.out.println("main() method call");

main(20);

} }

output:main() method call

                  20

 


note: In java, Methood Overloading is not possible by changing the return type of the method

Advantage of method overloading:

Method overloading increases the readability of the program.

Different ways to overload the method:

There are two ways to overload the method in java


a)By changing number of arguments
b)By changing the data type

Read More →