• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Singleton Creation

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

-prabhakaran
 
Sheriff
Posts: 22504
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please tell me what is singleton?
 
Marshal
Posts: 74349
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start with this FAQ about the Singleton Pattern.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic