How to overload the main() method in Java
KW.java
class KW
{
    public static void main()
    {
        System.out.println("Welcome to Kodingwindow");
    }
    public static void main(String args[])
    {
        System.out.println("Hello, World!");
        main();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Hello, World! Welcome to Kodingwindow kodingwindow@kw:~$
Example 2
KW.java
class KW
{
    public static void main(String args) 
    {
        System.out.println("Welcome to "+args);
    }
    public static void main(String[] args) 
    {
        System.out.println("Hello, World!");
        main("Kodingwindow");
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Hello, World! Welcome to Kodingwindow kodingwindow@kw:~$
Advertisement