jeudi 25 août 2016

my Class.forName is giving som issue in java 8

package designpattern.Singelton;

import java.lang.reflect.Constructor;

public class ReflectionIssueSingelton {

    public static void main(String[] args) throws Exception {
        Singelton s1 = Singelton.getInstance();
        Singelton s2 = Singelton.getInstance();

        print("s1", s1);
        print("s2", s2);
    }

    //Reflection

    //Load the class to be reflect
    @SuppressWarnings("unchecked")
    Class clazz =  Class.forName("designpattern.Singelton.Singelton");

    //getting degault declared constructor i.e default constructor
    Constructor<Singelton> ctor = clazz.getDeclaredConstructor();
    ctor.setAccessible(true);
    Singelton s3 = ctor.newInstance();


    private static void print(String name, Singelton object) {
        System.out.println(String.format("Object: %s, Hashcode: %d", name, object.hashCode()));

    }

}

enter image description here





Aucun commentaire:

Enregistrer un commentaire