The class keyword?
Table of Contents
The .class
keyword can be used in order to obtain a java.lang.Class<?>
object of a certain class. The .class
call is placed after the class name:
classvar = String.class
On instances of classes, calling getClass()
will return a java.lang.Class<?>
object:
aString = ""
classvar1 = String.class
classvar2 = aString.getClass()
assert classvar1 == classvar2//these both point to the same Class object
This also works for primitive types and arrays:
iclass = int.class
aclass = int[].class
There are a number of useful methods, mostly concerning reflection, which exist on instance objects of java.lang.Class
. For more information see the JDK documentation.