Singleton != Static

On Cedric’s latest entry he complains about statics, and quite rightly. However, one of the commentators missed the point.

The comment was:

What’s the difference between storing command line parameters in static variables and ensuring that the statics get reinitialised each time your program is invoked and storing them in a singleton class and ensuring that the singleton gets reinitialised each time your program is invoked?

Answer: none.

A singleton class has all the disadvantages of statics.

The point that the (anonymous) commentator misses is that Singletons and statics are not the same. Singleton is a creational pattern; it’s a design technique to ensure that there is only one instance. static, on the other hand, is something I’ve loosely dubbed an access pattern (though it’s really a technique). One can (and should) have Singletons, that aren’t static, and you can, if you work at it, have statics that aren’t Singletons,

The difference here is that with a Singleton, if you choose to no longer have a Singleton, it is easy to change. Typically the object that is the Singleton shouldn’t know it is one; it’s just a plain object, after all. Thus, configuration parameters (to borrow Cedric’s example) can be easily changed for another invocation of the object. In Cedric’s example, the right thing to do (as I’m sure he would agree) is to copy the parameters in the main() method into an instance of the object and execute that.

One of the strong anti-patterns in Java is the constant abuse of statics to hold state that really should be dynamic. Avoiding overuse of statics will generally give you a more flexible design.

Some good uses of statics, while I’m at it:

  • obtaining constants
  • factory methods (including the ubiqituous getInstance()!)
  • stateless methods (though this can often live better in their own class)

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 20 years now. I currently work for people, but my opinions here are in no way endorsed by them (which is cool; their opinions aren’t endorsed by me either). My main professional interests are in Java development, using Agile methods, with a historical focus on building web based applications. I’m also a Mac-fan and love my iPhone, which I’m currently learning how to code for. I live and work in Brisbane, Australia, but I grew up in the Northern Territory, and still find Brisbane too cold (after 22 years here). I’m married, with two children and one cat. My politics are socialist in tendency, my religious affiliation is atheist (aka “none of the above”), my attitude is condescending and my moral standing is lying down.

3 thoughts on “Singleton != Static”

  1. I did not understand about difference between singleton and static member.please send details with example to my Email id


  2. Manikandan,

    The Singleton pattern is about having only one instance of an object inside of a particular context (usually, but not always, an application-level context).

    A static member variable, however, is really a variable on the _instance_ of Class (which is just another object). In other words, it’s just a normal member variable, that happens to have a special syntax for accessing.

    In a simple Java application, you will normally have only one instance of Class per class you load. Because of this, a static member variable is usually unique.

    However, in reality, you can have one instance of a Class per _classloader_. A more complex application, particularly J2EE applications, can easily have multiple classloaders. In this scenario, you end up have multiple instances of static variables.

    As I note in the blog entry, if you use statics to store Singelton variables, you shouldn’t use statics to access them, if you’ve got any plans to grow beyond a simple one-class-loader application. Doing so will introduce bugs and other problems later down the track.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: