0

Possible Duplicate:
cracking singleton with other ways

Can anyone please tell me when a Singleton would not work as a Singleton?

Community
  • 1
  • 1
Sachin Tyagi
  • 27
  • 1
  • 4

3 Answers3

10

There is very good post published on Sun website by author Joshua Fox. Please go through this.

below are some of them scenario when your Singleton doesn't behave like.

  1. Multiple Singletons in Two or More Virtual Machines
  2. Multiple Singletons Simultaneously Loaded by Different Class Loaders
  3. Singleton Classes Destroyed by Garbage Collection, then Reloaded
  4. Purposely Reloaded Singleton Classes
  5. Copies of a Singleton Object that has Undergone Serialization and Deserialization
dma_k
  • 10,431
  • 16
  • 76
  • 128
amicngh
  • 7,831
  • 3
  • 35
  • 54
  • 1
    The link given by @amicngh appears to have changed since Oracle took ownership of Java. I believe the new link is http://www.oracle.com/technetwork/articles/java/singleton-1577166.html – HairOfTheDog Apr 21 '16 at 18:56
3

A singleton (in Java land) wouldn't work as a singleton if a given class is loaded by multiple class-loaders. Since a single class can exist (or can be loaded) in multiple classloaders, it's quite possible to have "multiple" instances of a "supposedly" singleton class for a given JVM instance. But this is a pretty rare case and doesn't happen a lot. :)

Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
0

If you are able to create more than one instance of the class then it doesn't remain singleton by using ways like cloning, De-serialization etc.

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63