Thursday, 9 February 2017

JAVA: Overloading of Main method in Java:



Overloading of Main method in Java:


We can overload main(),but JVM calls only standard/origional  method,it never calls our overloaded main methods...
Observe the below Program,
package com.rameshsoft.rameshselenium;

public class MainOverloadDemo {
         
          public static void main(String[] args) {
                  
          System.out.println("Standard main()");    
               
          }
          public static void main(int value)
          {
          System.out.println("Int type main()");      
          }
          public static void main(double value)
          {
          System.out.println("Double type main()");
          }

          public static void main(char value)
          {
          System.out.println("Char type main()");   
          }
          public static void main(String  value)
          {
          System.out.println("String type main()"); 
          }
}
OUTPUT:
Standard main()

Here JVM calls only original main(),not overloaded methods.
So if we want to call overloaded main methods ,we need call overloaded methods from original main().
package com.rameshsoft.rameshselenium;

public class MainOverloadDemo {
         
          public static void main(String[] args) {
                   System.out.println("Standard main()");
                   main('a');
                   main(10);
                   main(10.0);
                   main(200);
                  
          }
          public static void main(int value)
          {
          System.out.println("Int type main()");      
          }
          public static void main(double value)
          {
          System.out.println("Double type main()");
          }

          public static void main(char value)
          {
          System.out.println("Char type main()");   
          }
          public static void main(String  value)
          {
          System.out.println("String type main()"); 
          }
}
OUTPUT:
Standard main()
Char type main()
Int type main()
Double type main()
Int type main()

No comments:

Post a Comment