1

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

1
  • For every class there is an object that allows you to query some of the class's properties, create new instances of the class, et al. <ClassName>.class is the way you obtain a reference to that object. (And an interface is just a special type of class.) Commented Jan 5, 2012 at 4:28

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.