Why Main() Method Is Needed in Java Main Class

Why does the main function in Java reside in a class?

Probably for the same reason you put question marks at the end of a question: that's just how they decided it's done.

The main method is the result of a convention that says "this is how the entry point's method signature should look" which doesn't exempt it from language semantics.

Java does not support methods outside of classes/interfaces and as such it has to be contained in one.

What is the role of the class in which the main method is wrapped?

In Java, there's no way to have a method without an enclosing type. You need a class, an interface, an enum, etc. to be able to declare and/or implement a method. This is just how it works.

Even when you start a Java program, you specify the name of the class containing the main method. You don't just run arbitrary statements.

In other words it's about the program structure rather than about a specific role played by the class around the main method. And you can even use an interface instead of a class in recent versions of Java.

Why do some classes require main methods and others do not?

Every Java program (which is in turn, built up from one or more Java classes) requires a Main method. The purpose of this special method is to serve as an entry point to your program so that your program can be executed. More information can be found in this page.

In your Pie example, what happens is that when you run your application, the main method will be the first thing which will be called. Once that it will be called, it will create a new Object, named newPie using the Pie template (class) and so on.

Just as extra information, if you are using an IDE, if you add a main method in your Pie class with the given signature: public static void main(String[] args), the next time you run your program the IDE will ask you to select your entry point since it will have now found two entry points. Once that you will have made your selection, the IDE will make the necessary configurations so that the entry point of your application is noted.

Why does main method in Java always need arguments?

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.

Why should I put main() in a dedicated class?

It essentially boils down to Abstraction. You should try to give each class one role. Here, you have two roles that need to be filled:

  1. Load an object and display it
  2. Represent an object

Therefore you should use two classes. The advantage becomes more obvious once you start to want to use and display more than one type of thing in your program.



Related Topics



Leave a reply



Submit