Class.class and class.getClass, whats the difference

Discussion in 'Java' started by seyal.v.p@gmail.com, May 17, 2006.

  1. Guest

    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
     
    , May 17, 2006
    #1
    1. Advertising

  2. wrote:
    > 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?


    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(java.lang.String)
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getField(java.lang.String)

    Regards

    robert
     
    Robert Klemme, May 17, 2006
    #2
    1. Advertising

  3. Guest

    Thanks a lot for the response.
     
    , May 17, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. klauern
    Replies:
    9
    Views:
    7,083
    Steve W. Jackson
    Apr 12, 2005
  2. JavaEnquirer

    Generics and getClass

    JavaEnquirer, Feb 10, 2006, in forum: Java
    Replies:
    4
    Views:
    24,574
    darkaico
    May 4, 2011
  3. Philipp
    Replies:
    2
    Views:
    409
    Rolf Magnus
    Jun 29, 2006
  4. Dennis

    class/getClass()

    Dennis, May 11, 2008, in forum: Java
    Replies:
    5
    Views:
    423
    Mark Space
    May 11, 2008
  5. Paka Small
    Replies:
    19
    Views:
    1,173
Loading...

Share This Page