Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
4  
What else can it do? –  Pubby Mar 26 '12 at 15:37

3 Answers 3

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;
}
share|improve this answer

The main() method is an ugly throwback to procedural programming, providing the entry point into the application. Attempts are made in various programming languages to encapsulate it, but its very nature makes this difficult (it has to be public and static, but it should NEVER be called from anything else in the program, which is highly contradictory). WPF succeeded (by hiding main() from you deep in the bowels of the WPF application project and providing configurable "hooks" for custom processing), as did Java (in a similar way for Android apps), but WinForms and most other types of apps still make you deal with main().

So, most experts say that the LOC of the main() function should be as low as possible. There is one approach (which I think is slightly overkill) in which the main() function has one line:

public class Program
{
   private Program(string[] args)
   {
      //parse args and perform basic program setup
   }

   //Reduce the ugliness to the absolute minimum
   public static void main(string[] args)
   {
      new Program(args).Run();  
   }

   private void Run()
   {
      //kick off the driving O-O code for the app; i.e. Application.Run()
   }    
}

This is a little much, but I agree with the basic principle; main() should to as little as possible to get your object-oriented, event-driven application into a "ready" state.

share|improve this answer
    
I disagree. It can be useful to call main from other contexts- for example, recursion. –  DeadMG Mar 26 '12 at 22:35
1  
Personally if you're recurring your main method I think you should instead be calling another method and recurring that. Only in the simplest contexts (console app of roughly homework-level complexity/difficulty) would it be acceptable to call main() from within your program, and I would call that a trivial situation. –  KeithS Mar 26 '12 at 22:57

In languages that support functions main is just a regular function and so there's nothing else you can do with it other than what you said. And then there are idiotic languages that ditch functions in favor of having everything be an object, which means every time you want a function you have to wrap it in an unnecessary class.

Well, enough rambling. The point I'm trying to make is that Main is not really a class, but a function and so you shouldn't have do anything but parse inputs, create other objects and call other methods because that's all a function can do.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.