tags:

views:

188

answers:

7

You can actually just skip this first part cause it gets confusing, but I'll keep it here cause I want to know if anyone else feels the same way. I am a CS undergrad who has have been using Java for 3 years, and I still find it difficult to comprehend how to include the Main function into my design. It feels wrong to place it in a class all on its own, but it also feels wrong to insert it into another class file. I see the latter as such because it doesn't fit in with the rest of the class making it incohesive. I can't just stick a main function on my DocumentReader object, for example. It would have nothing to do with the object. On the other hand, I can't just make a Main class that only has a main function inside it because in object-oriented programming, you're supposed to think in terms of objects effectively creating a miniature world in your head. For that miniature world to revolve around a single object that does nothing but exist just seems off. It doesn't act as a narrator and character in a story because it does nothing, at the same time. I prefer C's style of having a Main function that is separate from everything. It keeps the narrative of the story separate from the characters that are interacting with one another.

I want to know how the professionals mix the main method into the rest of their code. How do you make it fit in with the rest of the design. Also, are they generally long or short?

+4  A: 

Well, I sometimes like to create an *Application class and put it there. For the DocumentReader, I'd have a DocumentReaderApplication where I would put the main function, and may also process any start-up options/command-line arguments. If it's a GUI app, the *Application class will launch the main window. After all, the program's entry point has to be somewhere, right?

FrustratedWithFormsDesigner
+5  A: 

I keep the main() function in a class named as my project. With this when you launch the application with the command line you type :

java com.domain.project.ApplicationName

It seems to me logic to have a simple Launcher name rather than :

java com.domain.project.AClassWhichDoesntLookLikeAMainClass

or the (too) classic

java com.domain.project.Launcher

But don't bother too much for this class, it will probably never be executed by the end user like this. A script or another executable will launch it most of the time.

Another thing, your main() method can get heavy, especially if you really use main args for more CLI options. Maybe it deserves its own class.

Colin Hebert
99% of the time Project name = Application name
TheLQ
Yes, but for the 1% sake a prefer explicitly say `project.ApplicationName`, even if it's redundant when project name = application name.
Colin Hebert
+4  A: 

A class with a main method acts as an entry point. So it's a good idea to put everything in that class, which is specific to that entry point. Delegate the rest.

Chris Lercher
A: 

I leave it in a Program class, where it's going to be all alone. It usually just kick starts my main window then exits when the window exits.

Blindy
+2  A: 

The main method is only used when you yourself start a stand-alone application. The world is moving away from this for large applications simply because they grow so big so you need to modularize.

Common approaches are WAR/EAR's for JEE, and OSGi bundles.

Thorbjørn Ravn Andersen
+3  A: 

Hardcore OOP people might not like this, but I just have a class called Main in my root project package that holds the main method.

My thinking is that your program needs to start somewhere, but putting it in another class is just confusing. I treat Main() as a special case that deserves its own class. I put nothing else besides setup information in this class. This makes it clear that this is the starting point (and sometimes the ending point) of my entire program.

TheLQ
I do that also.
Software Monkey
+2  A: 

I work in Java web development, and have for years.

I have never once professionally typed the words "public static void main".

Between ant tasks triggering unit tests and other local tasks, then deploying a WAR archive to a server, the main method doesn't fall into scope for web work.

Dean J