hi,
Is Class.class different from instanceOfClass.getClass() ?
I know it sounds blank. Let me explain the problem that I faced in
detail;
I have a class Class A with 2 protected methods and a private List
object.
Now in the test class of Class A, in a test case, I create a inner
class for Class A and override the two protected methods of Class A..
In addition I try to set up data in the List using relection.
i.e
testMtd(){
A a = new A(){
// Code for two overriden protected mtds here
};
Field f = A.class.getDeclaredField("NameOfTheListInClassA"); //This
works
Field f = a.getClass().getDeclaredField("NameOfTheListInClassA");
//This doesnt
}
Can anyone explain why?
Thanks in advance,
Seyal
Robert Klemme - 17 May 2006 09:06 GMT
> hi,
>
[quoted text clipped - 27 lines]
>
> Can anyone explain why?
This is not cause by a difference between Class.class and
instanceOfClass.getClass(). It's caused by the fact that a.getClass()
!= A.class, i.e. a is not an instance of A but of an anonymous sub class
of A. Note the difference between getDeclaredField() and getField():
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getDeclaredField(ja
va.lang.String)
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getField(java.lang.
String)
Regards
robert
seyal.v.p@gmail.com - 17 May 2006 09:34 GMT
Thanks a lot for the response.