aspose file tools*
The moose likes Java in General and the fly likes singleton class or to ahve a static variable inside a class 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 class or to ahve a static variable inside a class" Watch "singleton class or to ahve a static variable inside a class" New topic
Author

singleton class or to ahve a static variable inside a class

anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
Regarding having a singleton class and having a static object in the class, which is the better way to implement.
See i need to get the reference data from the database and store them in the hashtable. So whenever some method needs the data it will call that class and and if hashtable in not empty it will get the data from the database else get from the hashtable.
now i have two options
1) to have a class having a static hashtabe
The code goes like this..
public class refdata {
private static Hashtable data = new Hashtable();

public static synchronized String getvalue(String strKey) {
String value = data.get(strKey)
if(value == null) {
// get the details from the databese and populate the hashtable
String value = data.get(strKey)
}
return value
}
}
here i can directly write refdata.getvalue("test")

2)
To have a static class inside the class and have a private hashtable
public class CDBDQReferenceData
{
static private CDBDQReferenceData objReferenceData; //Singleton instance
static private HashMap hmpOutputCodeDetails;// the output HashMap of Code Details
private CDBDQReferenceData()
{
//code to put get the data from teh hashtable and put it into the hashtable
}
public static CDBDQReferenceData getReferenceData()
{

if (objReferenceData == null)
{
objReferenceData = new CDBDQReferenceData();
}
return objReferenceData;
}
public string getValue(String strkey)
{
String value = hmpOutputCodeDetails.get(strKey)
}
}
in this case i will get an object of this class and then i can call the get value to get the dateils
here i need to write
CDBDQReferenceData.getReferenceData().getValue("test");
what are the merits and demerits of using a singleton instance ??
Which method can be better ??
TIA
Anil
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Both are roughly the same. Both are going to have to be synchronized in order to avoid other people overwriting data on top of each other. Both have 1 instance for the entire program. The thing that the singleton has over the static class is that you control when the object is instantiated and thus you control when memory is used. You can make sure that the object is created only when you need it. Example: If I have a large object that uses a lot of memory, I may be concerned with when this object is created. Having a singleton create the instance only when you need it saves memory up to the point of use. On the other hand, if you have an object that uses a lot of memory and you create it no matter what, then your memory can be wasted. Consider if the class is static, then when the classes are loaded the objects will all be created and initialized at load time. The memory will always be used.
In reading over your solution, one thing you might want to be careful of is this. If you have a single instance of a Collection and this Collection persists, make sure that the Collection doesn't keep growing and growing and keep collection data. This could cause memory to never be reallocated back to the heap.
Regards,
Dale


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singleton class or to ahve a static variable inside a class