1

In java, what is the need/use of ".class" property of an object. e.g., MyClass.class. What does .class point to.

4

3 回答 3

1

An instance of the class Class.

http://javadocs.org/class

于 2013-06-14T22:27:05.790 回答
1

Sometimes you need some typing information, e.g. when you do some reflection.

The .class is specially handled by the compiler and is interpreted as a Class instance.

If you've used C# before this is the equivalent of the typeof operator.

e.g. if you want to dynamically get the full name of a type you can do:

System.out.println(String.class.getName());
于 2013-06-14T22:27:16.403 回答
0

在您想说“特定类的任何对象”的某些情况下,它可能很有用。例如,在 JUnit 中,您使用类的类对象来指示测试预计会引发特定类的异常。

它们在泛型的一些高级使用中也很有用,以确保方法仅适用于特定类型的对象。例如,SpringHttpMessageConverter对象使用它们。

于 2013-06-14T22:36:33.670 回答