What is the difference between getType () and getClass () in java?
As per the documentation for getClass and getType :
getClass
returns "A class object representing the execution class of this object."
getType
returns "a class object identifying the declared type of the field represented by this object"
The main difference is that it someObject.getClass()
will provide you with an object of the runtime class of type someObject
, and someField.getType()
will provide you with an object of the class declared to which it belongs someField
.
(The call someField.getClass()
will return Field.class
because it refers to the object itself Field
, not the field it refers to).
Also, while getClass
available for every object, getType
only available for objects Field
that are part of the reflection API.
source to share
getClass
is a method Object of a class that will be inherited by all classes and will work the same in any situation.
getType
is similar to another method written in different classes for different purposes.
getClass ()
Returns the runtime class of this Object. The returned Class object is the object that is locked by
static synchronized methods of the represented class.
As documentation AND it is generic to all classes that will ever be written in Java
getType () Returns different things in different situations
Such as,
In Character, the getType method Returns a value indicating a character general category.
In java.awt.Window the getType method return Enum Window.Type
source to share