1

Why the below snippet code use a non-static to run the program??? It there the advantages of running the program in that style???

public static void main(String[] args) { Main go = new Main(); go.start(); } public Main() { } public void start() { //SOME CODE HERE } 
7
  • 1
    public Main() in this case is a constructor, not a method. Commented May 17, 2018 at 6:38
  • It is using static method - the first one executed is public static void main(String[] args) then - inside this method - object "Main" is created and then non-static method start() is executed. Commented May 17, 2018 at 6:39
  • This programm still uses the static main method as its entry point. It just creates an instance of the class Main and then calls a method on it. You could also call this class Foo and the method bar or Peter and parker. This does not change anything. The entry point is always the main method. Why is it done this way? It's quite nasty to have a lot of logic in a main method. Hard to test, hard to work with. Commented May 17, 2018 at 6:39
  • Just want to ask why the code wanted to execute in the start() method but not the static void main(). Is there some different??? Commented May 17, 2018 at 6:42
  • @HuatLee have a look at the answer by GhostCat which as usual explains a lot of things in a nice and comprehensible manner. Other than that you can also have a look at this question with some more input. Commented May 17, 2018 at 6:44

2 Answers 2

5

Basically, there are two advantages of having a main() that simply instantiates an instance of the corresponding class, to then call methods on that object:

  • it allows you to "re-use" that Main class in a more object oriented way. If another class wants to use Main, calling a static method to get that going is most often not what you want (it makes unit testing harder for example to use static methods). Therefore, if "re-use" is one of your requirements, then making it possible to instantiate that class, and using it without calling its static main() can be beneficial.

  • beyond that, it also makes it a bit easier to unit test the main class as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe change the wording of "The one advantage" which (to me atleast) kinda implies that it's not really a good thing to do. Like "well, it's not a good thing but I guess the one advantage is..."
2

Classic object-oriented approach encourages encapsulation: everything must be local as much as possible and multiple instances of the class must be possible. Global is bad and ugly.

Object-oriented approach also encourages inheritance, polymorphism and possibility to override methods with well defined functionality. Or, alternatively, composition (compose alternative versions of the complex object from well defined sub-components).

While a single and simple static method looks not much different from a single and simple non-static one, it can only easily make calls to other static methods of this class and can only access static variables simply.

This blocks the advanced architectures that make no difference for the simple "hello world" but worth to consider if you want to grow up a big and complex application out of this stub.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.