Singleton Vs Static

Hello,

          I often come across this question, when will you decide you want a singleton class, when will you make your variables/methods static  ??

Thank You,
letsbedecentAsked:
Who is Participating?
 
aozarovCommented:
I think a Singleton is a better approach then having a utility class with only static methods.
One reason is that a Singleton can implement an interface and allow the Singleton Factory to return different singleton based on a given type.
That can happen per request or allow will allow you to change the implementation of the singleton without changing or compiling the classes who uses it.
0
 
neonlinesProject managerCommented:
The link explains completely about singleton and how to use ur static variables

http://www.javacoffeebreak.com/articles/designpatterns/
0
 
aozarovCommented:
This link has some more arguments that favour the Singleton aproach: http://www.odi.ch/prog/design/singleton.php
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
letsbedecentAuthor Commented:
>>That can happen per request or allow will allow you to change the implementation of the singleton without changing or compiling the classes who uses it.

If we use a utility class that has static methods, and change the methods implementation, we still need not change the classes that call this method right ??
0
 
aozarovCommented:
>> If we use a utility class that has static methods, and change the methods implementation, we still need not change the classes that call this method right ??
Right, but if you want to replace it with a different class that provides similar functionality.
0
 
letsbedecentAuthor Commented:
Is that a common need ??

is this the reason a singleton was conceived ??
0
 
aozarovCommented:
>> Is that a common need ??
It just make your application more flexible to changes (always a good thing to desing/code this way)

>> is this the reason a singleton was conceived ??
No, the main reason is to have only once instance per application.
There are several ways to achive it though Singleton is a more modular and flexible achive it.
BTW, to be precise it is not really a singleton per application but rather a singleton per classloader.
0
 
gnoonCommented:
I think it's useful when we need to get a reference of main frame at anywhere of code without passing the reference into the calling class.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I generally use Singleton classes for manager-classes like some database-manager, state-manager, etc and utility classes with static methods for simple utilities like String manipulations, date-time conversions, etc.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.