mardi 5 avril 2016

how to prevent multiple object creation using reflection

i have method that is used to return the instance of the object. Using reflection am getting the class instance that's fine. i need to avoid creating a new object every time for the same class. Do i missing something??

private static Object getInstance(String clazz) {//full path of the class in the clazz
    Class<?> c = null;
    Object obj = null;
    try {
        c = Class.forName(clazz);
        System.out.println("inside ins" + c);
            obj = c.newInstance();
    } catch (Exception e) {
        System.out.println(e);
    }
    return obj;
}

Object inst = getInstance("com.test.Test1");
Method method = inst.getClass().getMethod("getVal", String.class,String.class);
method.invoke(inst, "new params","ss");

thanks





Aucun commentaire:

Enregistrer un commentaire