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

后端 未结 4 2050
暗喜
暗喜 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:26

    The main class should be an entry point to your program and should thus be relatively small. However this all depends on your actual program. If it's 50 lines long, it might be overkill to create two files for it.

    As an example, consider the default Swing Application Framework Desktop Application as it would be generated by the NetBeans template, which is simple and short, and whose main() method is a single line that launches it:

    public class MyApp extends SingleFrameApplication {
    
        @Override protected void startup() {
            show(new MyView(this));
        }
    
        @Override protected void configureWindow(java.awt.Window root) {}
    
        public static MyApp getApplication() {
            return Application.getInstance(MyApp.class);
        }
    
        public static void main(String[] args) {
            launch(MyApp.class, args);
        }
    }
    

提交回复
热议问题