• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Singleton in Java

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Searching this board for "singleton" gives plenty of results. Here is a good recent one...

https://coderanch.com/t/376041/java/java/singleton
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some more resources on Singleton for you:
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/view?SingletonPattern
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).

Layne
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Jan
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic