When you do:
MyClass.class.someMethod()
What exactly is the \"class\" field? I can\'t find it in the API docs. Is it an inherited static fi
This is documented here:
http://java.sun.com/javase/6/docs/api/java/lang/Class.html
MyClass
is not the name of an object, it's a class name, so this is actually special syntax that retrieves the corresponding Class<MyClass>
object for the named class. It is a language feature, not a real property of the MyClass
class.
Please read :
A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a `.' and the token class. The type of a class literal, C.Class, where C is the name of a class, interface or array type, is Class. If p is the name of a primitive type, let B be the type of an expression of type p after boxing conversion (§5.1.7). Then the type of p.class is Class. The type of void.class is Class.
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.8.2
The .class
is not actually a field. You can think of is as more of an 'extension' like a file extension. It is a token used to differentiate the Class Object as opposed to an instance of the class.