Preferable location of main() method in Java class

2019-01-25 08:39发布

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

  • at the top before each field (to stress user its existence and force him to use it )
  • at the bottom (to let user see fields first and after that discover main)
  • after c-tor
    or ... .

Please share your thoughts, this is kind of stylistic/philosophical question. Please do not suggest to keep main() in separate file alone.

4条回答
走好不送
2楼-- · 2019-01-25 08:40

Sun Microsystems published their Code Conventions for the Java Programming Language many years ago and many organizations follow it to varying degrees.

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

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

This is IMHO a good reason to go with the methods-last approach. Regarding the placement of main among the methods, putting it first or last would work. If you find it "special" in some way, then put it dead last in the file.

查看更多
干净又极端
3楼-- · 2019-01-25 08:59

These are just my thoughts:

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

The thing is that in Java, everything (but primitives) is an object, so main() must be declared in some class somewhere. The code such a static method may execute is more concerned with setting up the program for execution, and delegating to our business logic (objects that actually do something) to run the application. As such, its concern is distinct from the rest of our class (which defines some data and behaviour that we are trying to encapsulate).

main() doesn't really belong with the data and behaviour of our everyday classes, as I doubt that every class needs to be executable on its own. main()'s concern is with running our program. As such, it should be declared away from our business objects, in a module of the project concerned with application launch/execution. So, as you might be guessing, I am proposing exactly what you've said not to suggest - keep main away from your classes and logic as much as possible, and only declare it in the context of an entry point to your application.

As to the location within a file itself, I don't really think it matters - as long as it is obvious that the code in that file is concerned with setting up and running the program.

查看更多
再贱就再见
4楼-- · 2019-01-25 09:03

I've always put it at the end, because that's how they do it in C. "Tradition". Which may not be that good of a reason. :-)

查看更多
Root(大扎)
5楼-- · 2019-01-25 09:04

I will suppose that you do not systematically put a main() method in every class you write (in this later case, you have to envision to write unit tests instead).

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

If this is the case, you should not have that many methods in your class. I personnaly always sort method by importance : the most important/usefull/central methods are above all other methods, that do not add any real job (setters and getters are of that kind to me).

This way, the reader has access to the most important information first.

Of course, coding using Java Convention, which I recommand, implies you first declare your class fields, before declaring your methods.

查看更多
登录 后发表回答