This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Singleton and Property File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton and Property File" Watch "Singleton and Property File" New topic
Author

Singleton and Property File

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Why do people prefer to have singleton class(class with private class) to read the property file/logging. Why not the static method in a class. What is the drawback of doing the samething using the static method of a class rather than creating just one instance of a class.
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

Whether to use a static class or a singleton is the kind of question that will bring geeks to blows as quickly as where to put curly braces.
FWIW, here are the tradeoffs I can think of offhand:
1.) initialization - a singleton can be initialized in the factory method, which may throw exceptions. While exceptions from constructors are allowed they are sorta' frowned on.
A static class can be initialized in a static constructor but when that gets called is less defined than the singleton factory and exceptions will end up as ExceptionInInitializer
2.) intent - a Singleton is probably the most common pattern and maintainers should know what the original developer intended when they see one
3.) the static class should have a private final constructor that will never get called (doesn't hurt anything, just useless code)
4.) syntax - pretty obvious but the syntax of a static method call is cleaner than calling the singleton factory and then the method

I suspect that the initialization is the primary reason you'd see singletons used for property file access.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Chris Beckey wrote:While exceptions from constructors are allowed they are sorta' frowned on.

Says who? It's very common to throw exceptions from constructors if the arguments are invalid. NullPointerException and IllegalArgumentException are thrown from constructors quite often.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Rob, Could you please tell us your point of view for using singletons with private construcor for reading properties file rather than the static methods.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Both have their pro's and con's.

Static methods:
Pros:
- no instance needed so a little less memory usage.
Cons:
- you can't use it anywhere an instance is needed.
- you can't extend any abstract class with abstract methods because you need an instance to use these methods.
- you are limited to only one single "instance" for life. If you ever need more "singleton" instances you have to rewrite code.

Singletons: well, switch around the pros and cons of static methods. The getInstance() method can be rewritten to return one of several instances; check out Multiton in this thread.
String.CASE_INSENSITIVE_ORDER is an example of the first pro for singleton; it implements Comparator<String>, so you can use it anywhere a Comparator is needed, like in TreeSet, TreeMap, Collections.sort, etc.

The second pro is something I've used quite a bit in my recent work projects. Summarizing:
The BaseClass provides the actual code, but some values are specific to sub classes. These values are provided by sub classes. But each sub class instance should only have one instance, so they become singletons. I can then use it:
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

It is possible to create partially constructed instances of derived classes if the base class constructor throws an exception.
Do a search for java constructor throwing exceptions for examples.
I said "frowned upon" and not forbidden or prohibited because this is a coding practice that may cause subtle bugs.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

Another advantage of a singleton is that if you're programming to an interface you can have the singleton implement the interface. You can't do that with static methods. This can make it possible, for instance, to replace the instance with a mock object to break the dependency during unit testing.

On the other hand, if you're really bothered about that, you're probably better off using a factory pattern instead. The factory can always return the same object if you want, giving you a de facto singleton, but you've got more flexibility.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Please explain more about "frowned upon"; I am not at all sure I agree.

I searched as you suggested. The first hit didn't look very good, but the second was this, which recommends Exceptions, exactly as Rob said earlier.
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

This article ExceptionsInConstructors has a couple of reasons why I would not (in general) throw an exception from a constructor. In particular the reasons are inaccessibility of resources created in a constructor and a potential security issue with partially constructed objects.
Just to be really clear though, I am not suggesting that this is a strictly forbidden practice but one that should be used with an understanding and acknowledgement of the potential issues.
 
jQuery in Action, 2nd edition
 
subject: Singleton and Property File