• 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

Instantiating the class containing the main method

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the reason why, but I feel uncomfortable instantiating the class containing the main method, as in this example:

It feels more natural to keep the main method in one class, and create a second class for the purpose of instantiation.
For example:

Either method works. Is there a reason to prefer one over the other?
Should I just learn to get over it?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer the second method because it gets you used to handling multiple classes.
If you create a command‑line application, you can remove the class with the main method and use its public interface behind a GUI. Then you put a new main method in, which calls the application via the GUI.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell's point is valid and the second approach is more technically robust but I suspect most people just put the main method in their application class. So I would say use the second version if you feel more comfortable with it but just remember most code you look at will probably be written along the lines of the first version.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect most books use the first method because the code is more concise. In real life, I use the second method. More often than not, the command line apps that we build tend to use some sort of IoC (either through our custom configuration driven factories, or Spring) to initialize the app, so it makes more sense to keep the main method that calls the IoC to initialize the app from the app itself.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic