class - Getting Declared Methods - Java -
class - Getting Declared Methods - Java -
suppose have next 3 classes
a.java
public class { public static void main (string[] args) { new c(); } }
b.java
import java.lang.reflect.method; import java.util.arrays; public class b { public b() { method[] m = this.getclass().getdeclaredmethods(); system.out.println(arrays.tostring(m)); } public void hello() { } }
c.java
public class c extends b{ public c() { super(); } }
and run main method in class a. well, should instantiate class c, in turn should phone call class b's constructor, print out declared methods. output []
. surprise me, expecting output (assuming classes in bundle called test
):
[public void test.b.hello()]
so, what's wrong? , how actual output?
getclass
returns class of instance. in case, c
. there no declared method in class c
. can solve problem using 'getsuperclass' method of returned class
object:
class c = this.getclass(); while (c != null) { system.out.println(arrays.tostring(c.getdeclaredmethods())); c = c.getsuperclass(); }
java class reflection
Comments
Post a Comment