This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Creation

 
Natesan Prabhakaran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can any one tell me how to create a singleton without private constructor.Thanks.

-prabhakaran
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without a non-accessible constructor you cannot guarantee that it is a singleton. If other code can call the constructor, it can create multiple objects and there will be no more singleton.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that would depend on your definition of "singleton".
If you defer object creation to some mechnism external to the "singleton" class, you could let that mechanism cache the first instance it creates and have it return that instance upon consecutive calls. Which is kind of how Spring handles "singleton" beans. Ofcourse this definition of a "singleton" is radically different from the traditional design pattern, and ouside the scope of such a mechanism the bean could not be considered a "singleton" at all.
[ June 05, 2008: Message edited by: Jelle Klap ]
 
Natesan Prabhakaran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not we use anonymous class to create singleton, by default its private constructor. I dont know please tell me.
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please tell me what is singleton?
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with this FAQ about the Singleton Pattern.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Natesan Prabhakaran:
Why not we use anonymous class to create singleton, by default its private constructor. I dont know please tell me.


I don't understand the question. Perhaps you can show us some code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic