About the “class” property/field

后端 未结 4 2110
后悔当初
后悔当初 2020-12-10 23:50

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

相关标签:
4条回答
  • 2020-12-10 23:54

    This is documented here:

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

    0 讨论(0)
  • 2020-12-10 23:57

    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.

    0 讨论(0)
  • 2020-12-11 00:06

    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

    0 讨论(0)
  • 2020-12-11 00:15

    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.

    0 讨论(0)
提交回复
热议问题