A.class与a.getClass

They are actually different with regards to where you can use them. A.class works at compile time while a.getClass() requires an instance of type A and works at runtime.

The .class Syntax

If the type is available but there is no instance then it is possible to obtain a Class by appending ".class" to the name of the type. This is also the easiest way to obtain the Class for a primitive type.

boolean b;
Class c = b.getClass();   // compile-time error

Class c = boolean.class;  // correct

Note that the statement boolean.getClass() would produce a compile-time error because a boolean is a primitive type and cannot be dereferenced. The.class syntax returns the Class corresponding to the type boolean.

Class c = java.io.PrintStream.class;

The variable c will be the Class corresponding to the type java.io.PrintStream.

Class c = int[][][].class;

The .class syntax may be used to retrieve a Class corresponding to a multi-dimensional array of a given type.

Retrieving Class Objects (The Java™ Tutorials > The Reflection API > Classes)

Retrieving Class Objects (The Java™ Tutorials > The Reflection API > Classes)
https://docs.oracle.com/javase/tutorial/reflect/class/classNew.html

原文地址:https://www.cnblogs.com/bitbitbyte/p/13028981.html