MyClass.class
and MyClass.getClass()
both seem to return a java.lang.Class
. Is there a subtle distinction or can they be used interchangeably? Also, is MyClass.class
a public property of the superclass Class
class? (I know this exists but can't seem to find any mention of it in the javadocs)

- 183
- 1
- 5
4 Answers
One is an instance method, so it returns the class of the particular object, the other is a Class constant (i.e. known at compile-time).
Class n = Number.class;
Number o = 1;
o.getClass() // returns Integer.class
o = BigDecimal.ZERO;
o.getClass(); // returns BigDecimal.class
Both cases return instances of the Class object, which describes a particular Java class. For the same class, they return the same instance (there is only one Class object for every class).
A third way to get to the Class objects would be
Class n = Class.forName("java.lang.Number");
Keep in mind that interfaces also have Class objects (such as Number above).
Also, is MyClass.class a public property of the superclass Class class?
It is a language keyword.

- 257,207
- 101
- 511
- 656
-
1What do you mean by a "Class constant"? When you assign "Class n = Number.class;" what would be the value of n? – Mocha Mar 18 '10 at 06:52
-
1n gets assigned an instance of Class, the object with describes the Number "class" (which is actually an interface). – Thilo Mar 18 '10 at 06:56
.getClass()
returns the runtime class of the object, so it can be modified when you change the class of the variable
.class
on the other hand always return the class constant of the "Type"
NOTE
If the runtime class happens to be the same as the TYPE class, both will be equal.
Example:
Long x = new Long(2);
Class c1 = Long.class;
Class c2 = x.getClass();
//c1==c2

- 9,604
- 3
- 32
- 53
-
".class on the other hand always return the class constant of the "Type" -> What does that mean? – Mocha Mar 18 '10 at 06:54
-
1
The MyClass doesn't have a static getClass method, in other words, you cannot call MyClass.getClass(), instead you need to call (new MyClass()).getClass() for it to work.
getClass() will return a MyClass.class object. So in other words, MyClass.class is the resulting object while getClass() is a method. The getClass() method is useful in cases where you do not know the actual class of the object, for example:
public void someMethod(Object o) {
if(o.getClass().equals(Set.class)) {
// The object is a set
} else if(o.getClass().equals(List.class)) {
// The object is a List
}
}
Note that the above code example isn't the best possible, I'm just trying to show how it could be used. The same functionality could be achieved with if(o instanceof Set) { ...}

- 6,733
- 10
- 37
- 43
What's the difference between calling MyClass.class and MyClass.getClass()
First of all your question title is a bit misleading! .getClass() is a method defined in java.lang.Object
so any object in java can call it where as .class is called on the class itself(like public static variables). So the question should be (sticking to java naming conventions)
What's the difference between calling MyClass.class and myClassObject.getClass()
Now to actual differences
.getClass() is a native java method in java.lang.Object. This method will return java.lang.Class object corresponding to the runtime class
of the object on which it is invoked. So
Test t = new TestSubClass();
Class c2 = t.getClass();
System.out.println(c2);
will print class TestSubClass
where as .class will return the statically evaluated (known at compile time) class. It is actually Class object corresponding to the reference type pointing to the actual object.So
Test t = new TestSubClass();
Class c2 = Test.class;
System.out.println(c2);
will print class Test

- 66,731
- 38
- 279
- 289