The .class Syntax in Java

In my new job we often use “.class” approach to get the name of some class. In the beginning I thought that “.class” is a some public field of class Object which every Java class extends from Object but it was wrong.

The “.class” syntax is part of Java Reflections. Here is the explanation of “.class” from Java Online Tutorial:
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.

It is a great fun to say on the primitive type int “int.class”, isn’t it? :) It works and returns “int” as text result to my surprise.

Here is the link for the .class syntax in the Official Java Online Tutorial.


About tsvetanv

Friends, Books, Music, Math, Programming.
This entry was posted in Java and tagged . Bookmark the permalink.

2 Responses to The .class Syntax in Java

  1. Matt says:

    Thanks for posting this; you provided me just the answer I needed. It was a bit hard to find this information since searching for ‘ ‘”.class”‘ doesn’t work very well on Google…I’d just end up finding a lot of intro material. Searching for ‘.class” syntax’ instead brought me here.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s