Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does main method in Java always need arguments?

Why does the main method in Java always need arguments? Why should we write String[] args every time, instead of just writing it when we use any arguments?

A method like this generates Main method not found compiler error. Since we never use any arguments to the main method, this should be allowed.

public static void main()
{
}

This is not an interview question. It just came to my mind while programming.

like image 453
dejavu Avatar asked May 28 '12 10:05

dejavu


People also ask

What is the purpose of arguments in main method?

Other arguments for the main method Since the main method is the entry point of the Java program, whenever you execute one the JVM searches for the main method, which is public, static, with return type void, and a String array as an argument. If anything is missing the JVM raises an error.

Why do we need String args in main method in Java?

When you run Java program, by right click on Java class with main method, it creates a Run Argument for that class. By the way, you can write your String args[] as String[] args as well, because both are valid way to declare String array in Java.

Why main method is always static in Java?

Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class. In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method.

Why main method is always void?

The program will be terminated after executing the main() method, and returning anything from the main() method is worthless because JVM will be done nothing for the returned object.


2 Answers

Basically, there are four answers:

  1. Because that's the way it was designed. Yea, I know that's a circular reason. But the point is that this is the way it is and it ain't going to change. So unless you are planning on designing your own language, the question is moot.

  2. Cleanness of design (aka the DRY principle). Don't specify two entry point signatures when one can do the job. And clearly, it can.

  3. Semantic simplicity. Suppose (hypothetically) that Java did support both void main(String[]) and void main() entry points. What would happen if a class defined both methods? Is that an error? If not, which one takes precedence when there is ambiguity? Is this confusing yet?

    By only recognizing void main(String[]), the JLS avoids that problem1.

  4. This is analogous to the standard C and C++ entrypoint signatures. (Admittedly, some C / C++ runtimes support other non-standard entrypoints as well, but that's not exactly a good thing2.)

None of this means that it would have been unambiguously wrong to do it another way. And for example C# gives you alternative signatures, and deals with the ambiguity problem by requiring the developer to designate an entry point some other way.

FWIW, this wikipedia page describes the "main" method in a number of languages.


1 - Though then you have the "problem" that people who are new to Java might guess (incorrectly) that multiple entry points ought to work, try it, and get a surprise. But I don't think any design could cope with "programming by guesswork".
2 - For a start, it is a portability issue.

like image 181
Stephen C Avatar answered Oct 23 '22 08:10

Stephen C


Because the java tool that runs the application looks for a main with a specific signature, so it knows it's calling the right one. Java has method overloading, so when looking up a method, you have to specify a fairly complete signature. Granted the java tool could do something more complex (look for the specific signature and, not having found it, look for any main and call it if it only finds one), but that's not what the Java designers decided to do (and subjectively, FWIW, I think that's for the best — keep it simple).

You can find the details in the Java Language Specification, Chapter 12: Execution. And note that as of when Java got variable argument lists, it became possible to declare main in two different ways:

public static void main(String[] args)
// or
public static void main(String... args)
like image 30
T.J. Crowder Avatar answered Oct 23 '22 08:10

T.J. Crowder