The moose likes Java in General and the fly likes Singleton in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton in Java" Watch "Singleton in Java" New topic

Singleton in Java

Ali Ekber
Ranch Hand

Joined: Jun 12, 2005
Posts: 41
Could someone please briefly explain what singleton in Java is or point me some online resources? I am being asked this question during interviews constantly. Thanks.
marc weber

Joined: Aug 31, 2004
Posts: 11343

Searching this board for "singleton" gives plenty of results. Here is a good recent one...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Hung Yee

Joined: Dec 22, 2004
Posts: 18
Some more resources on Singleton for you:
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112

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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ali Ekber:
Could someone please briefly explain what singleton in Java is or point me some online resources? I am being asked this question during interviews constantly. Thanks.

Note that Singleton is not a Java concept. It fits under the catgory of "Design Patterns" which are generic recipes, so to speak, to solve common problems that come up with many Object Oriented programming languages. I strongly suggest that you learn about Design Patterns in general as well as study how to implement them in Java (or any other language for that matter).


Java API Documentation
The Java Tutorial
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
I have to speak now against the doctrine.
Singleton is being portrayed as a design pattern, and has been for quite some time. Unfortunately, like a lot of things in Engineering/Science, the truth is in fact, counter-intuitive. Singleton is a design antipattern. That is to say that within the assumed context of achieving such things as software maintainability, clarity, ease of use, robustness, scalability, etc., the use of singleton implies a direct violation of requirements.

Granted, requirements analysis is not taught, or not taught very well, in education institutions (in my experience as a student and lecturer) and so this is a potential reason for common failure to acknowledge the obvious. It takes approximately 10 minutes of objective thought to arrive at the correct conclusion - the catch is abandoning the preconceptions and flawed reasoning that is portrayed in so many texts of today.

Tony Morris
Java Q&A (FAQ, Trivia)
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
Hello Tony,

I'm not sure if i follow you correctly.

I agree that Singleton has its pitfalls, and therefore certain danger of being misused. Easily forgotten thread safety, to name the biggest issue.

But I do not agree with the consideration that Singleton is an anti-pattern. As a matter of fact and from my knowledge, a Singleton is the by far most elegant ways to handle certain situations like for example resource management and connection pools. Oh, and logging usually relies on Singletons, too.

Can you please bring up a few facts? What exactly is a correct implemented Singleton violating?

many greetings,
I agree. Here's the link:
subject: Singleton in Java
It's not a secret anymore!