jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton Pattern" Watch "Singleton Pattern" New topic

Singleton Pattern

Arvind Varma
Ranch Hand

Joined: Dec 24, 2002
Posts: 48
can anyone help me explain "How a Singleton pattern implemented in JVM"?
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Sorry, I don't fully understand the question. Could you please elaborate? What do you need this for?

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
Arvind Varma
Ranch Hand

Joined: Dec 24, 2002
Posts: 48
I have been recently asked about singleton pattern which i aptly explained. Following that I was asked "how is singleton pattern implemented in JVM?"
I know that we can implement the singleton pattern for class A by checking if an instance is already present and then create it.
But i myself did not get that question..so posted it if anyone of you could help me out...
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
There are numerous variants. You can use lazy instantiation, so the Singleton is created only the first time you call getInstance(). In a web container, class reloading may mess up a Singleton based on static variables and you may be better off using the application context instead. This example allows only one instance of a class; sometimes you want one instance per possible state, the GoF book uses the example of using objects to represent the 26 letters of the alphabet; your Singleton will then have 26 instances, one for each letter.
- Peter
[ December 30, 2002: Message edited by: Peter den Haan ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Singleton Pattern
It's not a secret anymore!