aspose file tools*
The moose likes Java in General and the fly likes SINGLETON METHOD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SINGLETON METHOD" Watch "SINGLETON METHOD" New topic
Author

SINGLETON METHOD

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
Why do we need to make instance variable as static?
Why do we need to make instance variable as private?
Why do we need to mark constructor with private?
Why do we need to make instance variable as private?
Why do we need to mark method with static, synchronized?
Please brief me with when to use and where to use with 3 or 4 examples?
If somebody has already used this concept, please put some insight into it.

public class SingleTon
{
private static SingleTon instance;

private SingleTon()
{

}


public static synchronized SingleTon getSingleTonInstance()
{
if(instance ==null)
{
instance = new SingleTon();
}

return instance;
}
}
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Why do we need to make instance variable as static?
- static => class level. All instances share the same copy.
Had it not been static, every instance created of this class would have been composed of it's own private instance.
Why do we need to make instance variable as private?
- what if it is public? it's value can then be modified. The reference could be made to point to null by some malicious code. Result- creates another object on the next call to the same method.
Why do we need to mark constructor with private?
- what if otherwise? instances of this class can be created by invoking the constructor. No more a singleton.
Why do we need to make instance variable as private?
- this is a trick question. you just asked it few questions back. gotcha.
Why do we need to mark method with static, synchronized?
- "static synchronized". Obtains a lock on the instance of class java.lang.Class that represents this singleton class at runtime, when this static synchronized method is invoked. The idea is to try & ensure that only one instance is created. For ex: imagine 2 threads trying to create a singleton and the context switches just after the 1st thread finshes the if predicate. Both threads end up creating an instance - not a true singleton.
thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
Please brief me with when to use and where to use with 3 or 4 examples?
If somebody has already used this concept, please put some insight into it.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The Singleton pattern is very common. I'm sure you can use Google to find all the information you need. Also, you should get your hands on a copy of Design Patterns. It explains Simpleton and many other basic design patterns with all the detail you need. It also provides very good examples.
HTH
Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SINGLETON METHOD