Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Class.class and class.getClass, whats the difference

Thread view: 
seyal.v.p@gmail.com - 17 May 2006 08:42 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.