Take the 2-minute tour ×

What does .class mean in Java? For example, if I created a class called print. What does print.class return?

share|improve this question
*.class is the compiled byte code generated by compiling *.java files (assuming correct syntax) –  MadProgrammer Feb 26 '13 at 0:06
I'm afraid I don't fully understand what you're asking. If you could elaborate a bit more, someone might be able to provide you more useful information –  Paul Walker Feb 26 '13 at 0:22
This certainly looks like a real question to me. I was just asking this question myself, and found myself here after a quick Google search. I guess I shouldn't have bothered if I'd known that my question wasn't real! –  Doctor Jones Aug 17 '13 at 13:57
add comment

2 Answers

When you write .class after a class name, it references the Class object that represents the given class.

For example, if your class is Print (it is recommended that class name begin with an uppercase letter), then Print.class is an object that represents the class Print on runtime. It is the same object that is returned by the getClass() method of any (direct) instance of Print.

Print myPrint = new Print();
share|improve this answer
What are some of the things one can do with a class object? –  Kevin Feb 26 '13 at 0:34
@Kevin See the API documentation for java.lang.Class‌​. –  Patricia Shanahan Feb 26 '13 at 1:15
add comment

.class is used when there isn't an instance of the class available.
.getClass() is used when there is an instance of the class available.

object.getClass() returns the class of the given object. For example:

String string = "hello";

This will output

class java.lang.String

This is the class of the string object :)

share|improve this answer
getClass() is only useful if you have a reference pointing to an object of the class. ClassName.class can be used even if the class has no instances. For example, consider Math.class. Each is correct for certain situations. –  Patricia Shanahan Feb 26 '13 at 0:18
nice explanation... Thanks!!!! :) –  sunny Mar 2 at 14:03
add comment

Your Answer


