0

I've been reading up on singletons recently and I commonly see code like:

private static SingletonDemo instance = null;

Inside the Singleton class as a field.

I don't want particularly understand what 'static' is doing and why it's needed.

I read the Oracle site here. And they use a numOfBicycles (which is when you have multiple bicycles) example to justify using a static field, but I think I'm understanding something on why it's used when you're just instantiating 1 instance.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
user1413969
  • 1,261
  • 2
  • 13
  • 25
  • http://en.wikipedia.org/wiki/Singleton_pattern – Thomas Jan 29 '14 at 16:31
  • possible duplicate of [Difference between static class and singleton pattern?](http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern) – OldProgrammer Jan 29 '14 at 16:31

7 Answers7

11

The point is that you need a way of accessing the single instance which has been created - it's effectively a global variable. That's precisely what a static field achieves. (At least, it's global as far as the relevant classloader is concerned, which is all that's important most of the time.)

If it were an instance variable, then you could only find a reference to the singleton object if you already had a reference to the singleton object, making it pretty pointless.

Most singleton implementations have a static getInstance method or something similar. Consider how you would implement that without the static field, and you're likely to get a better idea of why it's needed.

(As an aside, I'd recommend against using the singleton pattern in most cases. Using dependency injection in a way which ends up creating only one instance in production, but where each class isn't a singleton per se typically ends up giving you more testable and flexible code.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

The static instance variable inside of a singleton is meant to hold the only instance of the class that will exist. It is static because it will need to be referenced by a static method 'GetInstance()' that will return the instance, or will create the instance if it is the first time that 'GetInstance()' was called.

I like to think of static as meaning that a member is shared across all classes instead of only allowing instances of a class to access their members.

Loothelion
  • 368
  • 1
  • 10
1

static ensures there is only one instance assigned to instance variable. If you are using a static getInstance() method, how would u assign value to a member variable in a sta5ic method?

Amit Sharma
  • 5,844
  • 5
  • 25
  • 34
1

As others have said, it's so that it can be accessed globally by other parts of your program. Singletons have uses for shared configuration data, so long as it is non-mutable, however they should be used with care.

If you are going to create a singleton I suggest using the enum method described by Joshua Block in Effective Java.

public enum MySingleton {
       INSTANCE;
   }

You then add all of your methods to the enum and access them by MySingleton.INSTANCE.myMethod(). The JVM will ensure that construction of the enum is synchronized and enforce that there is only one instance of the enumeration - so long as you only define one enum field.

Dodd10x
  • 3,344
  • 1
  • 18
  • 27
0

Statics do not require a instantiation of a class. In order to be able to count the number of times a class is instantiated you have to have a counter that is independent of the instantiations.

In the case of a singleton you want only one, but you could follow the pattern to make sure you have only x number of instationaions.

Pete B.
  • 3,188
  • 6
  • 25
  • 38
0

The purpose of singleton pattern is to create a single object of class that present anytime for accessibility that is why static field is used for this.

static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

Singleton's work on the principle of having one instance for the whole program scope. You create a static instance of the Singleton or static fields for the Singleton because you can allow your program to call for a new Singleton (if one hasn't be initialized) but you don't ever create more than one. These static fields ensure that the pattern is utilized properly, you can't be having multiple instances of a Singleton.

Mike
  • 874
  • 1
  • 8
  • 15