mercredi 5 août 2015

How to use reflection to invoke method in public members of a class in java?

Class A:

public class A {
    public B b;

    public A(String s) {
        b = new B(s);
    }
}

Class B:

public class B {
    String s;

    public B(String s) {
        this.s = s;
    }

    public void printB() {
        System.out.println(this.s);
    }
}

So i want to use reflection in java in order to create class A and use his public member B in order to run printB method using reflection with a given string from client 'printB'

How should i do it?





Aucun commentaire:

Enregistrer un commentaire