From the Java tutorial:
Finally, there's also a special kind of literal called a class literal, formed by taking a type name and appending "
.class
"; for example,String.class
. This refers to the object (of typeClass
) that represents the type itself.
To what type of variable can this literal be assigned to?
Please give a small example if possible.
In examples it is someting like that:
or
To understand that, you have to understand that
String
is aninstance (object)
of itssuperclass (parent class)
Object
.class String
'sinstance (object)
's value is aString literal
(e.g."I am a string."
) :whereas
class Object
'sinstance (object)
's value is aClass literal
— (e.g.Hashtable.class
) which refers toclass Hashtable
'sinstance (object)
To understand that, you have to understand that String is an instance (object) of the class Class. A string literal (e.g. "I am a string.") is a notation which represents an instance (object) of the class String, whereas a class literal (e.g. Hashtable.class) is a notation which represents an instance of the class Class.