Reputation:
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 field?
I thought reserved keywords were not allowed as entity names.
Upvotes: 1
Views: 2718
Reputation: 38531
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.
Java Language Specification: 15.8.2. Class Literals
Upvotes: 4
Reputation: 361595
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.
Upvotes: 2
Reputation: 39485
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.
Upvotes: 2
Reputation:
This is documented here:
http://java.sun.com/javase/6/docs/api/java/lang/Class.html
Upvotes: 0