Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a field class of type Class in Object

 
Carlo Smits
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I say
System.out.println(Object.class),
I get as output the toString method of the Class class. Is there a hidden field class of type Class in Object? Because in JDK documentation I do not see any field in Object.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Carlo
Object.class is a class literal. This is from JLS

15.8.2 Class Literals
A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a `.' and the token class. The type of a class literal is Class. It evaluates to the Class object for the named type (or for void) as defined by the defining class loader of the class of the current instance.

Also the wrappers clases and java.lang.Void class have a TYPE field containing a Class object for the corresponding type:
Integer.TYPE <==> Integer.class
...
Void.TYPE <==> Void.class
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good one, Carlo Jose.
Thanks.
- satya
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry I did a mistake in my previous answer.
The fields Integer.TYPE ... Void.TYPE holds references to int.class ... void.class objects.
Integer.class represents the Class object for java.lang.Integer class. Also Void.class is a class literal for java.lang.Void
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic