File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Singleton Creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Singleton Creation" Watch "Singleton Creation" New topic
Author

Singleton Creation

Natesan Prabhakaran
Ranch Hand

Joined: Jul 11, 2006
Posts: 47
Hi,

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

-prabhakaran


Regards,
Prabhakaran.N
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

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 ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Natesan Prabhakaran
Ranch Hand

Joined: Jul 11, 2006
Posts: 47
Why not we use anonymous class to create singleton, by default its private constructor. I dont know please tell me.
Santhi Bharath
Ranch Hand

Joined: Jun 03, 2008
Posts: 75
can you please tell me what is singleton?


thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Start with this FAQ about the Singleton Pattern.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton Creation