tags:

views:

80

answers:

4

When you do:

MyClass.class.someMethod()

What exactly is the "class" field?
I cant find it in the API docs
Is it an inherited static field?

I thought reserved keywords were not alowed as entity names.

A: 

This is documented here:

http://java.sun.com/javase/6/docs/api/java/lang/Class.html

+1  A: 

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.

John Kugelman
Class<|MyClass|> where |T| is the raw type of T, actually. If you had a variable `list` of type `List<String>`, `list.class` would return with a type of Class<List>.
Tom Hawtin - tackline
+3  A: 

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

Amir Afghani
So the whole expression MyClass.class represents one single thing i.e. it's not a member access.
A: 

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.

akf