Object-oriented – Should main method be only consists of object creations and method calls

object-orientedprogramming practices

A friend of mine told me that, the best practice is class containing main method should be named Main and only contains main method. Also main method should only parse inputs, create other objects and call other methods. The Main class and main method shouldn't do anything else. Basically what he is saying that class containing main method should be like:

public class Main
{
    public static void main(String[] args)
    {
        //parse inputs
        //create other objects
        //call methods
    }
}

Is it the best practice?

Best Answer

The point your friend is making is that an application should merely be bootstrapped by the main method, and nothing more. By having the main method in its own class, you are simply reinforcing that fact by keeping it independent of any application logic. The role of the main method would be to parse any inputs and to initialize the application with those, and possibly other, inputs.

public static void main(String[] args){
    new Foo().start(args[0]);
}

The idea is that you don't need the main method in order to initialize Foo. This allows you to easily initialize and start Foo in another context, potentially with different semantics.

public Foo initSomewhereElse(String arg){
    Foo f = new Foo();
    f.start(arg);
    return f;
}