Winston Gutkowski wrote:
manish ghildiyal wrote:Is my approach correct?
In 99.999% of cases: yes. The only possible exception might be if the object in question takes an extraordinarily long time to create or consumes a vast amount of machine resources, in which case the initialization on demand idiom might be more appropriate.
I maintain that the number is 100%.
Using lazy instantiation is never necessary. Even if it takes a huge amount of time and resources to create, if you need it, you need it, and you'll have to bear that cost of creation. And because
Java loads classes on demand anyway, you won't bear that cost until you're actually about to use the singleton anyway, so you won't be bearing it at program startup for something you never end up using. The only way you'd bear the cost unnecessarily is if you're using other static methods in that class, but not the singleton. And if you're doing that, then your design is broken anyway.