Link to home
Start Free TrialLog in
Avatar of drewmore44
drewmore44

asked on

General java architecture

Generally speaking, in a program that has several GUI classes and several other non-GUI "logic classes," what should be the relationship between those classes and the main method?

Intuitively, I feel like the main method should have very few lines of code itself, and should act as a "quarterback," calling methods and referencing variables in other classes in the appropriate order.

Again, I'm talking about IN GENERAL here, I know that each application will have different requirements and thus there is no blanket solution, but is this general strategy in line with best practices? Is there anything that a main method should always (or should never) have in it?

If an application has a main GUI window with a main menu that the user uses to launch other GUI windows, would/should/could the relationship between the program's main method and that main GUI window differ from the relationship between the main method and the other GUI windows?

I'm not necessarily looking for anything specific here, just trying to wrap my head around some basic concepts/practices.
Avatar of zzynx
zzynx
Flag of Belgium image

>> Intuitively, I feel like the main method should have very few lines of code itself
Correct.
Typically something like this:
public MyMainWindow extends JFrame {

  public MyMainWindow() {
      //... Build the content pane.
      Container content = this.getContentPane();
      content.add(...) // Add components to the content
      . . .
      this.setTitle("My new window");
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.pack();   // does layout of components.
  }

  public static void main(String[] args) {
       MyMainWindow myMainWindow = new MyMainWindow();
       myMainWindow.setVisible(true);
  }

}

Open in new window


>> Is there anything that a main method should always (or should never) have in it?
Well, it should take care that your main GUI window becomes visible.

>> If an application has a main GUI window with a main menu that the user uses to launch >> other GUI windows, would/should/could the relationship between the program's main >> method and that main GUI window differ from the relationship between the main
>> method and the other GUI windows?
I don't think I understand this one.
Intuitively, I feel like the main method should have very few lines of code itself, and should act as a "quarterback," calling methods and referencing variables in other classes in the appropriate order.
You're partly right there. If you'd stopped the above at the second comma, you'd have been completely right.

The main method is really just there to start the app. The app's own (non-static) methods are then used to take over. You can have a centralised method, say a run() method that can coordinate finer-grained method calls
Avatar of drewmore44
drewmore44

ASKER

Very helpful.

Is it ever useful to add attributes / methods to the main class (the class file where the main method is)? Or is this always a bad idea? Or is this "main class" not as formal a construct as I'm treating it - does it really matter what class the main method is in? is it helpful or even necessary to have a separate main class?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx 4 axxepting