Search code examples
javaprogram-entry-pointmethodology

Best coding (layout) practice for the "main() class" file?


Currently, my "main() class" file looks a bit like the following code. Rather than clutter up this example code with //comments for discussion, I have simply labelled four code lines with numbers (1 to 4), and these numbers refer to questions that appear after the code. Thank you.

// package myPackage;  // **1**

import myOtherPackage.*;

class mainProject{  // **2**

    // **3**
    private int myVar;

    mainProject(){
        myVar = 0;
    }

    public static void main(String args[]){

        // Keep main() looking fairly simple?
        // Perhaps just have some "essentials" here, such as error handling?

        new mainProject().start();  // **4**
    }

    private void start(){
        // The project gets going here..
    }
}

1 Unlike other class files in my project, I have not assigned a package name for my "main() class" file. Is this a bad design choice?

2 Is there a good naming convention for the "main class"? Is it helpful to incorporate the word "main" in to this class name? Would something roughly like "mainProject" be a good idea?

3 Various coding constructs can appear inside the main class file. For example, local variables, constructors, the main() method, and local methods. Do they have a "best order" in which they appear in this file?

4 Is it worthwhile to keep the main() method looking fairly "lean and simple"? In this example, I have just called a local private method called start(), which is intended to get the project started.


Solution

  • Ok, here is how I do it in my professional projects.

    For 1. every class should have a package. Main or no main makes no difference. Package is the way java organizes your classes at runtime in form of namespaces. So if you stop giving packages then you may end up with two class files with same name in the same folder or jar and when that happens, JVM picks the first class it finds by the name on the classpath. That may not exactly be the one you want.

    For 2. main (speciallypublic static void main(String[] args) is a specific and standard signature that Java needs. Any runnable program, a program that produces an output and can be executed needs a main method with this signature. I will try to explain the signature and that maybe will help you understand why it's like that.

    It's public because you want the JVM runtime code to execute the method. Using private or protected won't allow the JVM code to see your method.

    It's static because without static the JVM code would need an instance of your class to actually access the method. Remember that static methods and fields can be accessed by just using the class name. However non static members need a valid live object to reach them.

    It's void because main does not return anything to its caller. It's like any method having a void return type.

    And it's called main because the Java creators thought to give it that name. JVM runtime code which executes this method needs to know about the name of your method which will kick off the execution. Now, if I name it anything then it's impossible for the JVM code to make a wild guess. So name standardization called for a standard name and Java creators stuck to main.

    String[] is actually a string array containing the command line arguments that you pass to your program. args is the name of the argument and ironically this is the only thing that you can change to any name you want.

    For naming the main class, I usually prefer the names like MyProjectLauncher or MyProjectBootstrap where myProject is the name of your project like tomcat or bigben or anything you like.

    For 3. standard convention is:

    public class MyClass{
    
        //private members
    
        //protected members
    
        //constructors
    
        //private methods
    
        //protected methods
    
        //public methods
    
        //hashcode and equals
    
        //toString overrides
    }
    

    You can pick what you need and drop what you need. Public methods also include the getters and setter for your variables if you use them.

    For 4. When designing classes you need to keep in mind scalability and manageability of code. It's very common to have a main class and a few classes at start of the project and then when they grow into oversized kangaroos of thousands of lines then refactor code to adjust it. What you should do is create classes based on functionality, service helpers or actions. Keep main separate in a different class. Just use main to initialize a few things, parse command line options and delegate to start or initialize method which does the remaining things to kick off your program.

    Hope this helps.