Java - how much logic to put in the main class?

后端 未结 4 2143
暗喜
暗喜 2021-02-20 18:36

How much logic do you normally put in the main class? Should logic in the main class be at minimum, only instantiating other, specialized classes, and running all the tasks from

4条回答
  •  名媛妹妹
    2021-02-20 19:15

    It's not so much a question of whether a class is "the main class". It's a question of how much logic is in the public static void main(String args[]) method. Ideally, it should contain very little logic. It should essentially construct one or two objects, and then call methods on those objects. One of those objects might be a this() - an instance of the main class, and that's ok.

    You have to put the main() method somewhere - there's no need to create a special class just to hold that method.

    As a general rule, try to avoid having too much in static methods - static methods can't be mocked for testing.

提交回复
热议问题