This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
It's the same as that of a global variable in languages that allow it. It also comes with the same problems, and therefore should be used very carefully.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
The main reason for Singletons is to prevent multiple objects from attempting to access a resource of which there can be only one. Think of a piece of hardware, a logfile, or a network pipe that needs access control to prevent data corruption.
Moral: Singletons don't work unless you know where you are in the ClassLoader hierarchy
I thought it was an interesting point...
Joined: Oct 12, 2000
Of course. Singletons exist only in the context of a single classloader. Because each classloader is fully independent (even multiple classloaders inside the same JVM) and a class loaded by one classloader will even have another name as a class loaded by another (even if they're the same compilation unit!) you cannot guarantee that you're truly dealing with a single instance of a class (meaning a single instance created from a single classfile on disk across the entire application) unless you're certain that there's only a single classloader in operation that's loading your classes (there's always one or two more that you have nothing to do with, for example the classloader which loads the JVM itself from the native stub).
But for most programs you can assume that a singleton implementation will be unique at least within the JVM it's running in.