How much logic should be entered into the main class?

How much logic do you usually invest in a main class? Should the logic in the main class be minimal, only create instances of other specialized classes and perform all the tasks from there?

If you have any suggestions on this topic (or external articles), I would appreciate it.

+7
java
source share
4 answers

Should the logic in the main class be minimal only by creating instances of other specialized classes and performing all the tasks from there?

Yes. main and its surrounding class should ideally be used only as an entry point for starting a program. The mere existence of a surrounding class is just an artifact of how Java programs stack up (everything should be inside a class), and there is no reason why it should contain other things in addition to the main method (but there is definitely why it should not )

When you get interesting classes (those that make up the actual program) separated, you open the door for all kinds of flexibility. Perhaps some of these classes can be used in some other projects. You might someday want to replace some of them with better implementations. You might find a better order to instantiate all of these classes - so just swap a few lines. Or what about running long boot loads and instances in parallel threads? Just wrap some of them with suitable performers. Good luck trying it out with the main 1000+ class.

Such flexibility matters for everything except, perhaps, for 100-linear elementary examples, prototypes, and the like. But given that even small tools tend to grow, why not do it right from the start?

+4
source share

For small tools, I am glad that I have the main logic in the main class - there, as a rule, there are fewer models for work. (For very small tools, I admit, I usually do not understand unit tests. In particular, there is less benefit from the designer side of things than there is if you are building something that will be a component in a larger application.)

For large-scale applications, the main class is really just related to configuration and launch. If you are using a DI framework that can really be very small code; if you do not use dependency injection, then the main class often acts as a "manual" dependency injection environment.

+9
source share

This is not so much a question of whether a class is a โ€œmain classโ€. This is 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 for these objects. One of such objects can be this () - an instance of the main class, and this is normal.

You have to put the main () method somewhere - there is no need to create a special class to hold this method.

As a rule, try to avoid too many static methods - static methods cannot be mocked by testing.

+5
source share

The main class should be the entry point to your program and therefore should be relatively small. However, it all depends on your real program. If it's 50 lines long, it might be too difficult to create two files for it.

As an example, consider the standard Swing Application Framework desktop application that will be created by the NetBeans template, which is simple and short, and the main() method is one 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); } } 
+1
source share

All Articles