Preferred location of main () method in Java class file

When it comes to the order / sequence of methods in the java class. Where do you expect / prefer to see the main() method?

  • above each field (to emphasize its existence and the power to use it)
  • below (so that the user first looks at the fields and then finds the main part)
  • after c-tor or ....

Share your thoughts, this is a kind of stylistic / philosophical question. Please do not offer to save main() in a separate file only.

+8
java methods coding-style main
source share
4 answers

These are just my thoughts:

main () is a static method unrelated to object instances. We know that it exists as an entry point, which makes our executable program / class.

The fact is that in Java everything (but primitives) is an object, so main () must be declared somewhere in some class. The code that such a static method can execute is more about setting up the program to execute and delegating our business logic (objects that really do something) to run the application. So his concern is different from the rest of our class (which defines some of the data and behavior that we are trying to encapsulate).

main () is not really relevant to the data and behavior of our everyday classes, as I doubt that each class should be executed on its own. The main problem () is related to the launch of our program. Thus, it should be given from our business objects in the project module related to the launch / execution of the application. Therefore, as you can guess, I am proposing exactly what you said so as not to suggest - try as much as possible to avoid your classes and logic and declare them only in the context of the entry point into your application.

As for the location inside the file itself, I really do not think that it matters - as long as it is obvious that the code in this file is related to setting up and running the program.

+7
source share

Sun Microsystems published its code for the Java programming language many years ago, and many organizations follow it to one degree or another.

In this section, they suggest putting methods at the end of the file. And, as you know, main is "just another method", although the class method is instead of the instance method.

While no one forces you to follow the conventions of the Sun, there may be a slight advantage in sticking relatively close to them, since the degree of familiarity with it is known. Most (if not all) of the standard JDK libraries will follow it.

This IMHO is a good reason to go with the latest approach to methods. As for placing main among methods, this will work first or last. If you think this is "special" in some way, then put it dead last in the file.

+3
source share

I always did it at the end, because that's how they do it in C. "Tradition". Perhaps this is not so. :-)

+2
source share

I assume that you do not systematically put the main () method in every class you write (in this later case, you should imagine block tests instead).

As long as your class contains the main () method, as well as the entry point of your application, this class should not have any behavior other than initializing the application. This good practice is called "Separation of problems": one class = one responsibility.

If so, you should not have such methods in your class. I always always sort the method by importance: the most important / useful / central methods are primarily other methods that do not add any real work (setters and getters for me like that).

Thus, the reader has access to the most important information.

Of course, the coding using the Java convention, which I recommend, implies that you first declare your class fields before declaring your methods.

+1
source share

All Articles