2

I'm new to creating my own software. I'm a student so I've created things that have been layed out step by step but I've never really done the process myself.

I've been trying to implement good design techniques and make good use of OOP but I'm worried that the code I have isn't very well written.

My main worry is the main method. Where I'm making a chat program I created a class ChatClient to house the main method. But I've found that all the code I really need to put in the main method is a call to a JFrame class telling it to show the program's interface. From here the entire functionality of the program is handled in other classes.

Should I be pulling more control of the way the program runs into the main method? Or is it actually fine to have a single line for a main method?

DK_
  • 147
  • 1
  • 4
  • 11
  • 7
    a short main method for this is perfectly normal. once the jframe is instantiated the rest of program flow is event-driven. – Nathan Hughes May 16 '16 at 14:14
  • I think something like this is personal preference/opinion, and it doesn't really dictate whether you are following good OOP principles. You should be focusing on understanding principles like **polymorphism, inheritance, and encapsulation.** The way you lay out your main method as well as the remainder of your program should be based on good implementation of these principles. – A.Sharma May 16 '16 at 14:15
  • Generally, your main method contains one or two lines. If you have to do any command line argument (args) interpretation, you should do that in static methods performed from the main method. – Gilbert Le Blanc May 16 '16 at 14:31

5 Answers5

3

The main method and its surrounding class should ideally be used only as an entry point to start the program.

Each class you develop should be separate and have their own responsabilities since in the future they could actually be usefull on other programs/projects. You should always aim for a project with low coupling and high cohesion (more on this matter here: https://stackoverflow.com/a/14000957/6341202).

So, getting back to your original question, having only a line of code in the main method to initialize your JFrame is totally fine!

Community
  • 1
  • 1
Daniel Almeida
  • 372
  • 1
  • 14
1

In the main method the good practice is to call to other method that is who is going to execute the program.

public static void main(String[] args){
    Classname program = new Classname();
    program.start();
}

public program(){
    //The code that you want in the main
}
Asier Gomez
  • 6,034
  • 18
  • 52
  • 105
0

Instead of stuffing all the code in main() method its better if you write the code in a manner where it is loosely coupled and necessary modifications to the existing code are possible ie. maintainability

Swanand Keskar
  • 1,023
  • 1
  • 13
  • 27
0

Having single line of code in the main method is good enuough as long as you have written your work flow in other classes and methods using good OOPs concepts.

0

The main method is the only method to which you cannot assign a meaningful name. Therefore it should not do anything unexpected.

Ideally it contains only one function call. Exception handling and some logging (program version, build date) should also be fine. But not more.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45