0

I'm trying to create a class from a singleton class using reflection, but it doesn't work. My SingletonClass looks like this.

public class SingletonClass implements IHashBehaviour {
private static SingletonClass singleTon=null;

private SingletonClass () {
    // TODO Auto-generated constructor stub
}

public static synchronized SingletonClass newInstance(){
    if(singleTon == null){
        SingletonClass .singleTon=new SingletonClass ();
    }
    return singleTon;
}
}

public static SingletonClass create(SingletonClassType type){
    SingletonClass obj=null;
    Class<?> c;
    try {
        c = Class.forName(type.getFullQualName());
        obj=(SingletonClass)c.newInstance();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return obj;
}

The method getFullQualName returns the package, like this "model.SingletonClass". I understand why this won't work because in the factory, the newInstance method looks for the default constructor but that one is private. i need to call the newInstance method from the Singletonclass itself.

Bosiwow
  • 2,025
  • 3
  • 28
  • 46

0 Answers0