CodesJava

Easy learning with example program codes

can we overload main() method in java?


Yes, we can overload main() method. A Java class can have any number of main() methods. But it should have one main() method with signature as “public static void main(String[] args)” to run. If not class will compile but not run.

Example

/**
 * This class is used for main method overloading.
 * @author CodesJava
 */
public class MainMethodOverloding {
      //overloaded main method with one parm
      public static void main(int num1){
            System.out.println(num1);    
      }
 
      //overloaded main method with two parm
      public static void main(int num1, int num2){
            System.out.println(num1 + num2);
      }
 
      public static void main(String args[]){
            //method call
            main(20);
            main(10, 20);
      }
}

Output

20
30

Java interview questions on main method

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki