0

I got the below code from internet and even though there are 3 main methods, if I run the app from command prompt:

java MainTest 1 2 3

I would get the output:

String main 1

public class MainTest {

    public static void main(int [] args) {
        System.out.println("int main " + args[0]);
    }
    public static void main(Object[] args) {
        System.out.println("Object main " + args[0]);
    }

    public static void main(String[] args) {
        System.out.println("String main " + args[0]);
    }

}
  • Is there any scenario where more than 1 main method is needed in the same class?

  • I have not used yet the main method with int, Object [] args? Is that even possible? I thought it was only possible with String[] args.

  • I was expecting getting an error message at runtime. Why I did not get any error when the application ran? Does it mean that the parameters passed are always parsed to String and Java gets the main method that suits best?


Thanks a lot!!

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Cuban coffee
  • 294
  • 5
  • 14
  • Running by command line will always use that `main(String[] args)` the others one are just there to call from other classes i'm assuming. or when class loading and stuff – 3kings Dec 02 '15 at 05:39
  • You can overload as you like, but the main method called from java must be the `String[]` version (of which there are 3 compatible versions of syntax) – Bohemian Dec 02 '15 at 05:40
  • See this [question](http://stackoverflow.com/questions/13399836/can-there-exist-two-main-methods-in-a-java-program) – GAVD Dec 02 '15 at 05:41

6 Answers6

2

The example shows overloading

main method is overloaded.

JVM only looks for main method which is static and it should accept only one argument of String array. So you are getting String main as output

Is there any scenario where more than 1 main method is needed in the same class?

It only depends on your requirement

I have not used yet the main method with int, Object [] args? is that even possible? I thought it was only possible with String[] args

I think you want to call the main method with Object[] argument. public static void main(Object[] args) { is like any other simple method and you can call it by MainTest.main(new Object{1,2})

I was expecting getting an error message at runtime. Why I did not get any error when the application ran? Does it mean that the parameters passed are always parsed to String and Java gets the main method

The code is perfectly fine and so you are not getting any error.The code describes an example of overloading of main method

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

JVM will only recognize below as a main method.

public static void main(String[] args){} will be used when your class is launched by the JVM.

Also public static void main(String args[]) {} can be used by JVM.

public static void main(String[] args) {
        System.out.println("String main " + args[0]);
    }

rest are overloaded methods

Also we can use a varargs signature, as that's equivalent from a JVM standard

public static void main(String... args)

The positions of public and static may change.

From JavaDocs

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • Not actually true. There are 3 ways of coding a main it will recognise. `public static void main(String[] args)` is the usual one. The other two I leave as a puzzle for the reader... – Bohemian Dec 02 '15 at 05:41
  • 1
    `` it's `public static void main(String args[])` `` – Bohemian Dec 02 '15 at 05:55
  • 1
    `public static void main(String args[]) {}` – Unheilig Dec 02 '15 at 05:57
  • What is "done"? Your answer is still technically incorrect. – Bohemian Dec 02 '15 at 06:00
  • @Bohemian Can you please edit the answer, it's not clicking me – Ankur Singhal Dec 02 '15 at 06:02
  • 2
    @Bohemian: depending on what you count as a discrete way of encoding it. If you count the alternative placement of `[]` or varargs as a distinct way, I’d say there are more than three ways. E.g., making `public` implicit: `public interface Example { static void main(String... arg) {} }`. You may also insert annotations. I like this one: `public interface Example { @Target(TYPE_USE) @interface NonNull {} static void main(@NonNull String@NonNull... arg) { System.out.println("yes, it works"); } }`. And we didn’t talk about Generics yet… – Holger Dec 02 '15 at 09:08
  • @Holger I personally don't consider inserting annotations or interface methods as different ways of coding it. I mean ways that have the same signature - ie they would cause a `Duplicate method` compile error if you tried to code more than one of them on a class due to the fact that `String[] args`, `String args[]` and `String... args` are synonymous for the purposes of typing `args` to `String[]`. I'll admit variants of `public static void main(T[] args)` are an interesting curiosity I hadn't considered, but really they're different ways to specify `String`, not `String[]` – Bohemian Dec 02 '15 at 15:24
  • @Holger I meant that the compiler doesn't care about annotations when establishing method signatures, so adding annotations doesn't make a "new way" of coding a main method, any more than `public static void main(String[] foo)` does. – Bohemian Dec 02 '15 at 17:37
  • @holger you are missing the point completely, which is that the compiler doesn't distinguish between`String[] args`, `String args[]` and `String... args`, but they each use a **different syntax**. Adding an annotation doesn't use a different syntax to specify the parameter *type* – Bohemian Dec 02 '15 at 17:48
  • @Bohemian: if you narrow it down to the syntax and even further to the *array related syntax* (which isn’t obvious given your original statement “There are 3 ways of coding a main…”), then you’re right. But that’s why I asked, what you mean with your reference to the “Duplicate method compile error” as that didn’t help understanding that you are narrowing your original statement to array related syntax… – Holger Dec 02 '15 at 17:56
1

The name main is not reserved by the Java Programming Language. It’s an ordinary identifier and thus, the same rules as with all other identifiers apply. You can have multiple methods named main as long as they have different parameter types. That’s called method overloading.

The special meaning of a certain main method is defined by the Launcher which initializes your application and this startup procedure defines the rules. Note that there might be different, implementation specific, ways to launch an application, e.g. Applets don’t have a main method and JavaFX provides its own Launcher that works without a main method. The same applies to several server-side Java technologies. This doesn’t prevent you from declaring main methods when using such technologies, but they don’t have a special meaning.

So the well-known java launcher, using command line arguments, searches for a special method named main having the raw signature public static void main(java.lang.String[]) in the specified main class. Note that there were earlier Java versions whose launcher didn’t care about the presence or absence of the public modifier. All that matters to the launcher is, whether it finds such a main method. It doesn’t care about other methods of the same name and different signature. It also doesn’t care whether other classes have an appropriate main method. All it does, is searching for an appropriate method in the specified class.

Since only the raw (bytecode level) signature of the method matters, it is irrelevant, which source code form it has, as long as it got compiled to a method with the appropriate signature. Thus, the following declarations will lead to a main method, appropriate for launching a Java application:

public static void main(String[] arg) {}
public static void main(String arg[]) {} // source code variantion don’t matter
public static void main(String... arg) {} // varargs are compiled to an array type
public static final void main(String[] arg) {} // final is irrelevant

public interface Example {
    static void main(String[] arg) {} // public is implied (Java 8)
}

public static <T extends String> void main(T[] arg) {} // raw parameter type is String[]

Note that with the last variant, some IDEs failed to launch the application (despite it works from command line), showing that the actual behavior dependents on the implementation of the launcher.

In contrast, the following doesn’t work:

class String{}
public static void main(String[] arg) // the parameter type is not java.lang.String[]

demonstrating, that not the source code form but the compiled raw signature matters. When trying to run the last example, the launcher will complain that it doesn’t find the required method public static void main(String[]), not telling you that there is a different, non-matching main method. As said, it doesn’t care about other methods of that name.

Holger
  • 285,553
  • 42
  • 434
  • 765
0

In java the method which is having exactly below signature :

public static void main(String[] args)

Having special rights and considered the Main method. Other are just overloaded methods.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
0

you are defining simple methods, JVM will not call them directly except public static void main(String[] args) . so it's upto your requirement in which you need a method with overloading concept and it's static so it can be called directly using the class name.

as per my understanding, it's up to your requirement only.

mayank agrawal
  • 628
  • 5
  • 20
0

You can write as many static main methods as you like, but the JVM is looking for a specific main method signature.

public static void main(String[] args)

Any other main method will exist (and can be called by some other Java element) but those other main methods will not be called by the JVM at startup time.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138