aspose file tools*
The moose likes Beginning Java and the fly likes singleton object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "singleton object" Watch "singleton object" New topic
Author

singleton object

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
I dont understand what is singleton object. I looked online and it says that singleton class is a class that can have only one object at a time. What does this mean ? I dont think it means we can only creatte one object of a class. Can anyone pls explain preferably with example.
Please also explain Factory class too.
Thanks
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

An example of a singleton is an application object. You might have more than one window, but you wouldn't have more than one application object.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
Actually I have the same doubt in mind, but I am not convinced by the above post. Also another thing, I've never heard of something called a Factory Class do you mean to say factory method instead or rather a static factory method then this blog
has a good explanation I got recently.
Also I bear a doubt in my mind,
How to write a singleton class.

Thanks in advance.


SCJP, SCWCD.
|Asking Good Questions|
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

There had been a discussion on same a few days ago in this same forum.
Please view this thread on topic to have more details.

PS. It is a good practice to check FAQ's of the forum and search the forum using the Search button on top to check if your question had been already answered. Please take time to the same before posting.
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Look the following code. It's the canonical implementation of a Singleton. The private constructor avoids other classes to instantiate Singleton objects. The only way to get a Singleton instance is calling the method getInstance(), that will always return the same instance.
Of course, there are some issues regarding distributed systems or concurrence.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Er, there's some rather vital "static" keywords missing from that "canonical" implementation of singleton.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11411
    
  16

in brief... a singleton means you can only create the object one time within your program. say i had a car rental program. one of my classes would be a Car. clearly, i'd want to create many, many instances of cars, assuming i have more than one to rent.

I may also have a scheduler class. this is the thing that says "on this day, this car will be used by this person". if i had two instances of that, they might both fight for the same resource. instead, i design my code so that everybody gets a reference to the same instance. everyone is talking to the SAME scheduler.

a factory class is a class who's job is to create instances of other classes. to corrupt the HFJ example, i may have a PizzaFactory class. you pass its method a parameter, and it returns a PepperoniPizza, a SausagePizza, or a CheesePizza.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Originally posted by Peter Chase:
Er, there's some rather vital "static" keywords missing from that "canonical" implementation of singleton.


Sorry, my mistake

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1773
    
    7

Originally posted by Peter Chase:
Er, there's some rather vital "static" keywords missing from that "canonical" implementation of singleton.


That, plus the fact that the "canonical" example of a Singleton usually employs lazy instantiation to create the single instance, but that's splitting hairs really


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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Originally posted by R Lopes:
Sorry, my mistake

You're not the only one.

Log4J has the exact same problem with their NullAppender. There is a method called getInstance() with the following comment:
Whenever you can, use this method to retreive an instance instead of instantiating a new one with new.

Slight problem: that method is NOT static
So, in order to call a method to prevent creating a new object, you have to create an object first. Nice try guys


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
If you create a class with private constructor, how do you instantiate that class, if its not an inner class ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

From within the class itself, as in R Lopes' example.
neilson ramalho
Greenhorn

Joined: Apr 07, 2008
Posts: 10
Originally posted by jignesh soni:
If you create a class with private constructor, how do you instantiate that class, if its not an inner class ?


As Rob Prime said, you instantiate the object inside the class, at this point

So, outside your class, you use getInstance method to get a reference to this object, i.e,

So, just to remeber,
"Private constructors prevent a class from being explicitly instantiated by callers"
[ April 08, 2008: Message edited by: neilson ramalho ]
 
 
subject: singleton object