14

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.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
Roman Ivanov
  • 2,477
  • 3
  • 20
  • 31
  • 2
    Why *not* keep the main() in its own file? If you don't want to use OOP then don't use java... –  Oct 27 '11 at 04:53
  • This is kind style to avoid mixing arguments parsing and any kind of preparation before call like "job.doAll()". – Roman Ivanov Oct 27 '11 at 05:19
  • thanks to all for your opinions. Our team decided to put main to the top of class as it is entry point to the class functionality. – Roman Ivanov Apr 09 '13 at 21:09

4 Answers4

8

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.

filip-fku
  • 3,265
  • 1
  • 21
  • 19
  • thanks, we do keep main() is special launchers classes, but we have one project that contains number utils classes that could be launched from by main() - it is convenient. But creating separate/additional class that configure and launch business class will double number of classes and will make code less easy. So we in search for compromise to keep main() in the same class but still keep class easy to ream and understand how to work with him. – Roman Ivanov Oct 28 '11 at 01:34
7

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. :-)

user949300
  • 15,364
  • 7
  • 35
  • 66
5

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.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
1

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.

Olivier B.
  • 305
  • 1
  • 2
  • 10