Home » Java programming language

Can we overload main() method in Java?

Here, we are going to learn, can we overload main() method in Java? Is it possible to overload main() method in Java?
Submitted by Preeti Jain, on July 16, 2019

The question is that "can we overload main() method in Java?"

  • Yes, We can overload the main() method in Java.
  • JVM calls any method by its signature or in other words JVM looks signature and then call the method.
  • If we overload a main() method in a program then there will be multiple main() methods in a program. So JVM calls which method? we don't need to confuse if we have multiple main() methods then JVM calls only one main() method with (string[] argument) by default.

Example:

class MainMethodOverloading {
    public static void main(String[] args) {
        System.out.println("We are in String[] args");
    }

    public static void main(int args) {
        System.out.println("We are in int args");
    }

    public static void main(String args) {
        System.out.println("We are in String args");
    }
}

Output

E:\Programs>javac MainMethodOverloading.java

E:\Programs>java MainMethodOverloading
We are in String[] args

By default JVM call only one main() method of a String argument, But if we want to call another main() method or any other overloaded main() method, then we can do only one thing that is we can call overloaded main() method explicitly.

We can call other main() methods inside the original main() method with a String argument.

Example:

// Java Program to demonstrate overloading of 
// main() method
import java.io.*;

class MainMethodOverloading {
    // Origional main() method
    public static void main(String[] args) {
        System.out.println("Hi, We are in main (String [] args) ");
        MainMethodOverloading.main("Call main() with one argument");
    }
    // These are the overloaded main() methods 
    public static void main(String args1) {
        System.out.println(args1);
        MainMethodOverloading.main("call main() with", "two argument");
    }
    public static void main(String args1, String args2) {
        System.out.println(args1 + args2);
        MainMethodOverloading.main("call main() with", "three argument", "from two argument main()");
    }
    public static void main(String args1, String args2, String args3) {
        System.out.println(args1 + args2 + args3);
    }
}

Output

E:\Programs>javac MainMethodOverloading.java

E:\Programs>java MainMethodOverloading
Hi, We are in main (String [] args) 
Call main() with one argument
call main() withtwo argument
call main() withthree argumentfrom two argument main()





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.