• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reflection api

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In the below reflection api code

in line 1 we are assign the reference of an List interface; In second line we are invoking
getInterfaces() which is actually defined in the java.lang.Class.

Compile time should be ok as the reference type is Class and contains this method at runtime we have reference of List
Interface then how is this working?

Thanks
Chandra
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have an instance of List. You have an instance of Class representing List. If it wasn't, then the first line would never compile. java.util.List.class is a class literal, which is how you get a Class object if you know at the time of writing which one you want.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You have an instance of Class representing List



What does this mean? a Class representing an Interface

-Chandra
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words,



produces:
interface java.util.List
interface java.util.RandomAccess
interface java.lang.Cloneable
interface java.io.Serializable
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chandra shekar M wrote: What does this mean? a Class representing an Interface


java.lang.Class is just a class, just like ArrayList, String, JButton etc. Instances of java.lang.Class correspond to actual classes or interfaces.

So String.class is an instance of java.lang.Class representing String. List.class is an instance of java.lang.Class representing List. And so on.

Methods like getInterfaces() belong to the java.lang.Class class - it's this class that's at the heart of the reflection API. So ArrayList.class.getInterfaces() returns java.lang.Class objects representing the interfaces that ArrayList implements.

(I've fully qualified java.lang.Class in the above description to try and make it clear when I'm referring to that, and when I'm referring to the word "class" more generally)
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks, I read JavaDocs its saying java.lang.Class will represent Class and Interface Types, at runtime JVM will use this Class and types specified and will
retrieve the required info. Thats fine Was curious to know how this process happens at Runtime any links will be help full.


Thanks
Chandra

 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Class cls = java.util.List.class;

Java.lang.Class represents both interfaces and Classes. Then in te above code JVM represents the List Interface as List.class means JVM is representing Interface as an Class is that right?

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic