Maxi
Maxi

Reputation: 285

What is meant by Interface.class in java?

When it comes to java, Suppose I is an interface, What is meant by I.class? And what is it use for?

Upvotes: 1

Views: 1254

Answers (3)

samkass
samkass

Reputation: 5951

The "class" member of an interface is most useful when creating objects via reflection (using a ClassLoader and the class's name) to check for methods and see if a new class isAssignableFrom a given interface.

Upvotes: 2

erickson
erickson

Reputation: 269657

It's the Class instance for the interface, I. With it, you can find the methods and fields of the interface, check to see if objects implement the interface, etc.

Type.class is syntactic sugar for obtaining object Class object via Class.forName(). Although the "class" might seem out of place when applied to an interface, it works the same way, and provides much of the same information. You won't be able to reflect on any constructors, however.

Upvotes: 2

Aravind Yarram
Aravind Yarram

Reputation: 80176

Every class or interface you write is associated with a java.lang.Class object during runtime in Java. All instances of the same class share the same Class object and you can obtain the Class object by calling the CLassName/InterfaceName.class or obj.getClass() method of the corresponding object. Read the What is the use of class “java.lang.Class” ? article for more details.

Upvotes: 4

Related Questions