5

I have noticed in java programs at least, that people tend to prefer to start their programs by creating and instance of the class that contains the main method, and activating a non-static method within it, like so

    class MainClass {
        void start() {
            //start the program flow
        }
        public static void main(String[] args) {
            new MainClass().start();
        }
    }

Than to simply start the program flow through the main method from a static context, like so

    class MainClass {
        public static void main(String[] args) {
            //start the program flow
        }
    }

I was wondering, why do people prefer the former method as the latter seems simpler, and is there something that should determine which one I should use?

1
  • 4
    Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question.
    – gnat
    Aug 13, 2015 at 6:11

4 Answers 4

6

Both approaches, can be considered fine.

There are several causes for your question.

The "I come from another Programming Language" approach

Remember that some developers learnt other Object Oriented Programming Languages before learning Java, and keep some programming styles, while programming Java.

C++ Is good example. A C++ programmer that uses classes and objects, has to use a main function, where an independent object must be instantiated first.

Other Java only programmers, may use the ´static´ methods approach, that was designed by the original Java designers, and that appear in several books, and tutorials. To those programmers, allocating an independent object may be seen unnecessary.

The "Let's use some modularity" aproach

The "independent object" approach is also used as a modularity technique, as a way to keep programming logic more simple.

2
  • 1
    This doesn't really explain why people would choose the additional level of indirection, though. Aug 13, 2015 at 17:11
  • How does knowing Java influence things here??? Does it make you comfortable with using procedural code where appropriate, uncomfortable with static functions, or what? Aug 15, 2015 at 19:51
3

Your first example seems to describe someone who perceives public static void main(String[] args) as merely an entry-point, a "punch-in" point for the operating system to call, and nothing more than that. It's a perfectly valid point of view.

In my latter years as a Winforms developer, I began using form events as a "patch panel" instead of places to put logic. Rather than littering the Form class with business logic, I would declare a method such as "Submit.OnClick," and immediately defer the execution of that method to another method in another class. This promotes decoupling and modularity; I can focus solely on form logic in my form class, and non-form logic in the methods I'm calling elsewhere.

In this particular example you provided, I'm not sure that it's buying you all that much.

1
  • If you are doing this because you think of public static void main(String[] args) as an entry point, would it then be equally valid to have the only thing it does be to start another static method, to start the program flow?
    – Phoenix
    Aug 16, 2015 at 8:53
1

It depends on what the Main class and what the start() method are supposed to represent.

Remember, public static void main(String[]) ultimately is just a hook for the JVM to start your application with:

java -cp ... Main [args]

It could very well be that Main is an utility class inside a larger framework/library, that has its own main() method so that it can be started independently outside of its conventional usage. Or a main() method was simply supplied for some kind of debugging purposes (hypothetical scenario), which may be removed without notice in future releases. In other words, whatever is done within the main() method does not - and will not - affect the features of the Main class.

Alternatively, maybe you are just looking at too many "introduction to Object-oriented programming" sample classes, where the instantiation of the Main class and calling its start() method is meant to prepare learners on what OOP is about.

So how should you determine what to use? (not a hard-and-fast rule)

If you are creating a simple application with no need for OOP, such that all your methods are actually static and you no longer depend on a class's 'state' (fields), you should stick to the simpler approach as well. Otherwise, or if you are grasping the OOP concepts, then you should do it the more OOP-like way of new Main().start().

-2

Your class (though it is main class) is not always supposed to be executed by jvm directly. May be in future some other class needs to launch your application or to start() object of your class. So people use former method taking care of future in thier mind. Now which one you should use depends on class and your application use.

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