aspose file tools*
The moose likes Beginning Java and the fly likes Instantiating the class containing the main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiating the class containing the main method" Watch "Instantiating the class containing the main method" New topic
Author

Instantiating the class containing the main method

Ed Dablin
Ranch Hand

Joined: Oct 09, 2012
Posts: 32
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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
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.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2400
    
  50
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.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2447
    
  28

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiating the class containing the main method