Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton

 
Arathi Raj
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
How do you make a class to be singleton? What does this mean. Can u explain it?

Arathi
 
Joshua Bloch
Author and "Sun God"
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arathi,

See "Effective Java" Item 2. It's in Chapter 2, "Creating and Destroying Objects." I think that chapter is available online.

Josh
 
Arathi Raj
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joshua Bloch:
Arathi,

See "Effective Java" Item 2. It's in Chapter 2, "Creating and Destroying Objects." I think that chapter is available online.

Josh


Hi Couldn't find the sample chapter on Creating and Destroying Objects. If possible Explain the concept.

Thanks
Arathi
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arathi Raj:
Hi
How do you make a class to be singleton? What does this mean. Can u explain it?

Arathi


A singleton class is which will have only one instance for the entire application. It means there will be only one Object of a particulat class can be created in a application.

You implement this by making the constructor as private. Sample code looks like this:
public class SingletonClass {
private static SingletonClass singletonClass = null;

private SingletonClass() {
// restricts the user not to create object
}

public static SingletonClass getSingletonClass() {
if(singletonClass == null) {
singletonClass = new SingletonClass();
}
return singletonClass;
}
}
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arathi Raj:
Hi
How do you make a class to be singleton? What does this mean. Can u explain it?

Arathi


Try googling for "Singleton". The first few links give decent descriptions. If you still don't understand, come back with specific questions about the problems you encountered. We will be happy to clarify from there.

Layne
 
fdafd fdafda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the wikipedia explanation quite easy to understand

http://en.wikipedia.org/wiki/Singleton_pattern
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI: Singleton looks interesting in some aspects. But, may need to be programmed with care, if you are introducing aspects like hashmaps inside it and making the hashmap bigger over the time. That can make it a memory hog.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic