Possible Duplicate:
What’s the difference between calling MyClass.class and MyClass.getClass()
Wanting to have a Class<T>
object, Which is the difference between these two approaches?
Test ob = new Test();
ob.getclass();
or
Test.class
As per Class javadoc
First approach get Class object from Object
.
second approach get Class object for a named type (or for void) using a class
literal.
The getClass()
method is defined in java.lang.Object
and hence can be called on any object reference.
It gets the Class object associated with the run-time type of the object to which the reference points. .
The getClass() method is not really analogous to .class at all. Closer is Class.forName(String), which gets a Class object for the class named by the String.
In situations where either could be used, use .class
, as it is more efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With