Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

class is a singleton or not

  Asked By: Anita    Date: Jul 24    Category: Java    Views: 811

written a class which reads a .properties file and the
class should be a singleton. i dont know how to check the wether the
class is a singleton or not, can any one check the file and let me
know wether the written class is a signleton or not.
I appreciate ur Help.

Here the java file is, followed by .properties file

import java.io.FileInputStream;
import java.util.Properties;
import java.util.*;

public class PropertiesTest {

private PropertiesTest() {}

private static PropertiesTest MySingleTon = null;

public static PropertiesTest getInstance()
if ( MySingleTon == null ) {
MySingleTon = new PropertiesTest();
return MySingleTon;

FileInputStream propFile = new FileInputStream
Properties p = new Properties();

Enumeration keys = p.propertyNames();

while (keys.hasMoreElements())

String key = (String)keys.nextElement();
String value = p.getProperty(key);
System.out.println( " the property value of " + key + " and
value is " + value);
}//end of while
}catch(Exception e){e.getMessage();}




1 Answer Found

Answer #1    Answered By: Husani Chalthoum     Answered On: Jul 24

Your singleton  is not  thread safe...

either you need to synchronize the getInstance method or maybe you should
initialize it in the declaration of the MySingleTon variable instead?

Also you dont have to use static  when using a singleton (after all the
MySingleTon var is already static) do all your initialization in
the constructor of the singleton instead. This is the template i use (may
contain typoes since i just need to sleep instead of compiling but since
sleeping is boring, im here typing this message... oh well):

class Singleton {
private static Singleton _inst = new Singleton();
public Singleton getInstance() {
return _inst;

// Other props
private int aValue;
private string  anotherOne;

// constructor - initializes
private Singleton() {
aValue = 5;
anotherOne = "five";
// etc

public synchronized void setAValue(int val) {
aValue = val;
public int getAValue() {
return aValue;

Note that the only member being static is the instance of the Singleton,
everything else should not be. If you have methods (e,g, setters) for
updating data in the singleton you MUST synchronize them or you may have

My singleton can be tested like this:

Singleton s1 = Singleton.getInstance();
/* now lets drop s1 and get a completely new instance... um or is it? ;) */
Singleton s2 = Singleton.getInstance();
System.out.println( s2.getAValue() );
// and we should get a nice 7, not a 5 or anything else

I'd also like to raise a warning here. Singletons are not always a good
solution to your problems, in an EJB environment they may cause more harm
than help  since a singleton is specific to it's classloader by default and
EJB uses several classloaders even several machines thus making strange
things happening to some singletons.

For your property  loader class  you could also check  out
java.util.ResourceBundle especially if you want to work with multi language

Didn't find what you were looking for? Find more on class is a singleton or not Or get search suggestion and latest updates.