lundi 20 février 2017

How to use consumer and supplier instead Reflection in java 8

I have a two simple class and use reflection pattern for invoke method.

public class Sample {
    public void a() {
        System.out.println("Call Method a");
    }
    public void b() {
        System.out.println("Call Method b");
    }
}

public class SampleMainClass {
    public static void main(String[] args) {
        try {
            Object o = Class.forName("Sample").newInstance() ;
            Method method = o.getClass().getDeclaredMethod("a");
            method.invoke(o);                
        } catch (Exception e) {
            System.out.println("Now i do not known  this method");
        }
    }    
}

output

 Call Method a

So i heard in the java 8, reflection pattern is deprecate and instead that we can use consumer and supplier.

How to use consumer and supplier instead Reflection in java 8> Thanks.





Aucun commentaire:

Enregistrer un commentaire