How can I call the method of an object that has already been loaded into the JVM using reflection? I tried
Class myClass = Class.forName("myClass"); Method m = com.test.class.getDeclaredMethod("getValue",new Class[] {}); Object result = m.invoke(myClass,null);
but I get java.lang.IllegalArgumentException: the object is not an instance of a class declaration . The method I want to call is invalid, i.e. Does not accept parameters
UPDATE I have an application that has already loaded class "A". Another class βBβ will be created using the framework. When class "B" is initialized, class "A" is already loaded into the JVM. I want to call a method from a loaded instance of class "A", but without reference to "A" in class "B". In the answers, it seems that I should create a new instance of βAβ in class βBβ, but I want to access an already loaded object. If I create a new instance of "A" in "B", why do I want to use reflection? Did I miss something?
thanks
source share