• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to structure simple java programs?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a little confused about how to structure simple programs (i.e. one-method algorithms for a simple purpose). There are a few different ways that I have been doing it, but I want to be consistent. Should I put both the one-method algorithm (e.g. a factorial function) and the main method that executes the algorithm into the same class and then export it to an executable .jar file for use? Or should I create one class for the algorithm and another for the main method that executes the algorithm? In addition, is there any reason that I should out these classes in a package before I export it?

As another similar question, if I have constructed two distinct classes with two separate purposes, and they are both used in the construction of a single program, then would it be best to just put the main method in a third, separate class or should I put it in one of the two classes?

These are simple novice questions that I need answering. Thanks!
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My opinion is that if you are going to reuse the functions at all, they should be in a different class from main. In general, the class with main() in it has only simple code to get a program started.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

If those really are functions I think they should go in a utility class by themselves.
Utility classes:-
  • 1: Only static members.
  • 2: No need to create any instances, so only a private constructor.
  • 3: They contain only functions and constants. Except possibly for private members.
  • Agree with Knute: the main method belongs in a class by itself.
     
    Miles Davis
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Knute Snortum wrote:My opinion is that if you are going to reuse the functions at all, they should be in a different class from main. In general, the class with main() in it has only simple code to get a program started.



    So since programs can get complex pretty quickly, with multiple classes, would it be best as a general rule just to keep the class with the main method distinct from the classes with the program code?
     
    Bartender
    Posts: 689
    17
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you have a very simple program as you describe then its perfectly fine to put the main method and the other method in the same class. There is no point over-complicating things.

    * Edit: as others have pointed out if you plan to reuse the code in other applications then its best to move the main method to its own class. By simple program I mean cases where you might be just testing something out etc.

    However as your applications get more complex it does make sense to move the main method into its own class. The application I'm currently working on has the main method in a class on its own. The main method verifies the right number of command line parameters are present, then constructs an instance of another class and calls a method on it to start the application.

    There are no hard and fast rules though.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Miles Davis wrote: . . .
    So since programs can get complex pretty quickly, with multiple classes, would it be best as a general rule just to keep the class with the main method distinct from the classes with the program code?

    Unless your program is very small, yes.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    More about that in our FAQ.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic