Question

After reading gnat's answer to Why a static main method in Java and C#, rather than a constructor? I take his answer to mean that the purpose of a Java class with a static main method is to define a program entry point and that it is not meant to be the program itself.

There may be a better way to do this, but I usually have the class with the static main method to do something simple like this:

public class MenuLauncher
{
    public static void main(String[] args) {
        Menu menu = new Menu();
        menu.run();
    }
}

Would the code above be the best practice for OOP where the class with static main doesn't do much more than launch or start program logic contained within a separate non-static object; after-all main is static so wouldn't the MenuLauncher class itself be very limited? Since main is a starting point I don't see any other purpose for the class other than to be a point of entry.

Is there a Java naming convention commonly used for classes that contain a main method and serve the purpose of being a program entry-point?

Was it helpful?

Solution

No, there is no widely used naming conventions for this. Examples I have seen are Main, Application, XLauncher or X, where X is the name of the project/application.

And yes, I think it's good for this class to contain only the minimum logic/code necessary to set up the application and start it. But I'm sure there are a lot of God Objects and Big Balls of Mud out there with a main method tacked onto a multi thousand line monstrosity.

OTHER TIPS

They are called the Main-Class, whether or not they are called Main and whether or not they do little or much.

They usually have the logic required for starting up the program.

In CLI apps they usually validate parameters.

In Desktop app they open the main menu like yours or open the main window.

If you read the MANIFEST files in a jar file, you would find they are called that.

Manifest-Version: 1.0
Created-By: xxx
Built-By: xxx
Build-Jdk: 1.6.0_04
Main-Class: myPackage.MenuLauncher
Licensed under: CC-BY-SA with attribution
scroll top