简体   繁体   中英

Can we overload the main() method in Java?

The following code simply prints the word "hi" when run.

import java.util.*;
import java.io.*;

class poly
{
    public static void main(String c)
    {
        System.out.println("enter a char");
        InputStreamReader ir=new InputStreamReader(System.in);
        BufferedReader br =new BufferedReader(ir);
        //char l= br.readLine();
        System.out.println("this is "+c);
    }

    public static void main(String args[]) throws Exception
    {
        System.out.println("hi");
    }
}

Is there a way to overload the main() method?

Your program only starts at one location, so that makes no sense. Furthermore, polymorphism is a totally different concept; that's called overloading , not polymorphism.

What you are trying to do is overloading the main method, not making it polymorphic. And no, you can't do it (or to be precise: overload you can, just the JVM won't call the overloaded versions). The JVM is looking for a main method with a specific signature, namely taking a String[] parameter.

Maybe if you tell us more about the actual problem you are trying to solve, we can offer alternative solutions.

In brief you can overload a main function like:

class A
{

    public static void main(String s) {
        System.out.println(s);
    }

    public static void main(int s) {
        System.out.println(s);
    }

    public static void main(String args[])
    {
        System.out.println("inside main entry");
        main("me");
    }

}

Output:

inside main entry

me

We can have any number of main method and that can be overloaded but the main thread of an application will always start at the method with the signature:

       public static void main(String[] args)

and nothing else.

In Java args contains the supplied command-line arguments as an array of String objects.

The code is correct, you've overloaded the main method. But, as Peter mentioned, the main thread of an application will always start at the method with the signature

 public static void main(String[] args)

and nothing else. For starting an application, JVM will ignore all other main methods. To execute the content, you'll have to call it in your code, like so:

 public static void main(String args[]) {
    main("me");
 }

(Should print "this is me" to the console)

I'm not quite sure what you are asking but I don`t see any reason why you cannot overload the main method. The "main method I think you are referring to is

public static void main(String c)
{
    System.out.println("enter a char");
    InputStreamReader ir=new InputStreamReader(System.in);
    BufferedReader br =new BufferedReader(ir);
    //char l= br.readLine();
    System.out.println("this is "+c);
}

To overload this method I would create a method such as

public static void main(){}

This must be in the same class as the other main method to make it overloaded. The static keyword means common to the class.....(there is no need to use an object to access the method from the main(String[] args) method.

In my ind creating an overloaded method in a class is the same as creating a different method. Different arguments, parameter list ====> different signature ======> different method.

I hope this helps.

You can overload a main method in Java; however, getting the classloader to start from the overloaded main method is going to be quite a trick.

The class you pass the the java executable is inspected, it's static methods are read, and control is passed off to only the one that looks like

public static void main(String[] args) { ... }

Since this is a static method, and Java does not have the concept of inheriting static methods, you have no way to overload this method. Since Java does not have the concept of inheritance of static methods, sub-classing the static bits of a class is a nonsensical idea.

Maybe you want something like this?

import java.util.*;
import java.io.*;

class poly {

   public static void main(String c)
   {
      System.out.println("enter a char");
      InputStreamReader ir=new InputStreamReader(System.in);
      BufferedReader br =new BufferedReader(ir);
      //char l= br.readLine();
      System.out.println("this is "+c);

   }
   public static void main(String args[])throws Exception
   {
       if (args.length == 1) {
          poly.main(args[0]);
       }
       else {
          System.out.println("hi");
       }
   }
}

The java runtime environment only looks for the exact match to public static void main(String[]) , which is defined as the entry-point for standalone java applications.

To simulate the behaviour you want you have to call the other main yourself:

/**
* Your method called main, ignored by the java runtime since
* it does not match the signature. Called by the valid main(String[]) 
**/
public static void main(String c)
    {
    System.out.println("enter a char");
    InputStreamReader ir=new InputStreamReader(System.in);
    BufferedReader br =new BufferedReader(ir);
    //char l= br.readLine();
    System.out.println("this is "+c);

}
/**
* Starting point of your program, called by the java runtime.
*/
public static void main(String args[])throws Exception
    {
    if(args.length >= 1){
       main(args[0]);

    }else{

        System.out.println("hi");
    }
}

As others point out having several methods with the same name but different parameters is called over* loading * and not polymorphism (over* writing *).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM