0

Is overloading of main method with string []args and varargs possible??if not then why??i tried following code which gave compilation error.please help .i m new to java.

public class obj1 {

    public static void main(String ... args) {
        System.out.println("main method varargs");
    }

    public static void main(String[] args) {
        System.out.println("main method string arrays");
    }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
sasha
  • 11
  • 4
    Variadic arguments (`...`) are really just syntactic sugar for an array argument. As such, you are trying to define two methods with the same signature. – Andy Turner Jun 30 '16 at 09:55
  • Also here explained: http://stackoverflow.com/questions/301563/difference-fnstring-args-vs-fnstring-args – RichardK Jun 30 '16 at 09:57
  • @AndyTurner you should write that as an answer, not a comment :) – sotix Jun 30 '16 at 09:57
  • 1
    @sotix T.J.Crowder wrote a better answer :) – Andy Turner Jun 30 '16 at 09:59
  • I'd be surprised if this weren't a duplicate (though oddly I haven't found one yet), but it certainly *isn't* a duplicate of http://stackoverflow.com/questions/3759315/can-we-overload-the-main-method-in-java. RichardK's http://stackoverflow.com/questions/301563/difference-fnstring-args-vs-fnstring-args is much closer but doesn't address trying to have both in the same class. – T.J. Crowder Jun 30 '16 at 10:17
  • 2
    @T.J.Crowder What about this one? http://stackoverflow.com/questions/12375823/method-overloading-with-argument-vararg-of-object-class-and-array-of-object-clas – Tunaki Jun 30 '16 at 18:05
  • @Tunaki: Excellent. – T.J. Crowder Jul 01 '16 at 06:23

1 Answers1

5

In Java, both of those methods are really public static void main(String[] args) (fundamentally). That's why it won't compile; you can't have a class with two public static void main(String[]) methods.

Java added variadic arguments by having the function compiled as accepting an array, and then by making calls to the function automatically wrap the variadic arguments in an array.

That is, if we have

void foo(String... args)

called with

foo("one", "two", "three");

what the compiler actually creates is

void foo(String[] args)

called with

foo(new String[] { "one", "two", "three"})

(The compiler marks the method in the class file so that it knows later that the array at the end is variadic, but the code it creates is for a method accepting an array.)

This is mostly covered by JLS§8.4.1:

The last formal parameter of a method or constructor is special: it may be a variable arity parameter, indicated by an ellipsis following the type.

...

The declared type of a formal parameter depends on whether it is a variable arity parameter:

  • If the formal parameter is not a variable arity parameter, then the declared type is denoted by UnannType if no bracket pairs appear in UnannType and VariableDeclaratorId, and specified by §10.2 otherwise.

  • If the formal parameter is a variable arity parameter, then the declared type is specified by §10.2. (Note that "mixed notation" is not permitted for variable arity parameters.)

...where §10.2 is talking about arrays.

In fact, it's perfectly valid to call a foo(String...) method with an array instead of discrete arguments. Example, suppose we have:

private static void foo(String... args) {
    for (String s : args) {
        System.out.println(s);
    }
}

we can call that either like this:

foo("one", "two", "three");

or like this

foo(new String[] { "four", "five", "six"});

Both are perfectly valid; example: http://ideone.com/a1Ku37

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875