I know getClass()
method in java.lang.Object
. But In my application, I know all of my classes. So, What is the actual purpose of using this method?
7 Answers
But In my application, I know all of my classes. So, What is the actual purpose of using this method?
So if you write this, do you still know what the real class of things[0]
is?
Object[] things = new Object[]{
"Hi mum", new Integer(42), /* and a whole bunch more */};
shuffle(things);
In theory, you could work out the class of things[0]
using a long series on instanceof
expressions, but that is painful. This solves the problem.
Class cls = things[0].getClass();
Now, I'll grant you that you don't often need to do this sort of thing. But when you do, the functionality of Object.getClass()
is pretty much indispensable.
And as other answers point out, the getClass()
method is very important for applications that make use of reflection to get around the limitations of static typing.
I guess, the other question is whether the existence of the Object.getClass()
method has any impact on JVM performance. The answer is No. The method is only exposing information that has to be there anyway in order to implement the instanceof
operator. And this information is typically used by the garbage collector.

- 698,415
- 94
- 811
- 1,216
-
Hey look he answered the question and the answer to life the universe and everything! – mohdajami Sep 13 '11 at 08:28
-
I guess medopal just enjoyed your reference to [42](http://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe.2C_and_Everything_.2842.29); I don't think it was meant in a negative way. Oh, and +1, BTW (not for 42 but for a good answer). – Heinzi Sep 13 '11 at 12:03
-
@medopal - my apologies ... I'm sometimes a bit "slow" at the end of the day – Stephen C Sep 13 '11 at 12:44
You're wading into the wonderful world of Reflection. Using reflection, you can programmatically inspect classes, set their fields, call their methods, etc. You can do some really powerful stuff with it, and most frameworks that you use on a daily basis (e.g. Hibernate, Spring, Guice, etc.) depend very heavily on it to make things happen.

- 10,675
- 1
- 45
- 50
-
4Good overview. But I think it's important that while *frameworks* can do wonderful stuff with it, classical business code should generally *avoid* using it (at least directly), because it can *easily* make your code structure an unreadable mess. – Joachim Sauer Sep 13 '11 at 07:17
-
2getClass is typically used in business code when overriding the equals method, to check that the class of the other object is the same as the class of this object. – JB Nizet Sep 13 '11 at 07:30
getClass()
is a public method on Object, so it's inherited for all instances of all classes. If you are dealing with a super type of some sort (a super class or interface etc) and you want to know the implementation class, getClass()
will tell you the class of any object. You can then inspect the class to find out more information.
A simple example:
public static void doSomething(Object obj) {
if (obj.getClass().isArray()) {
System.out.println("It's an array");
} else {
System.out.println("It's not an array");
}
}
btw, getClass()
itself isn't part of reflection - it's a straightforward method that returns a straightforward (class) object. However, the class object returned from getClass()
can be used with reflection (see the java.lang.reflect
package) to find out lots of interesting things about the class.

- 412,405
- 93
- 575
- 722
-
9"nothing whatsoever" except that it returns a type that is about **the** central type in any reflection code. – Joachim Sauer Sep 13 '11 at 07:20
-
@Joachim Sauer OK - you're right. That was a bit over the top. I've tempered my post :) – Bohemian Sep 13 '11 at 11:49
You can use Class
objects for reflection, or you can use them for other things, such as keys into a map for keeping data about things at a class level.
For instance, you may have a class with static state & methods that do things like instantiate objects and gather statistics. In fact, you may have several such classes that all do the same thing and all seem to operate in the same way, and thus have possibilities for convergence. Since the class is static, you've naturally got a mapping from a class to some state and operations on that state.
Thus, you could extract the common code from your static classes and put them into a class that provides all the methods, with the addition of a Class
argument to specify the state that should be used during following operations.
But In my application, I know all of my classes. So, What is the actual purpose of using this method?
Now that you've got a system that operates on Class
objects, you can give it objects that you don't own, such as 3rd-party libraries that you decide to use in your application, and you can do it at runtime!

- 9,141
- 6
- 43
- 65
The JavaDoc states:
The returned Class object is the object that is locked by
static synchronized
methods of the represented class.
It means, if your classes has a static synchronized
method, the JVM uses the getClass()
to put a lock on the method.

- 87,898
- 29
- 167
- 228
-
There is no evidence here that the JVM actually calls getClass() for this purpose. It seems probable but it is not implied by the Javadoc. – user207421 Sep 13 '11 at 08:14
-
1@EJB - The quoted javadoc text is an **explicit uncategorical statement** that this is what JVM does. The language used in the quotation leaves no room for "maybe / maybe not". – Stephen C Sep 13 '11 at 10:30
Sometimes we need the Class
instance for an object. Like we program against interfaces but need to now the actual type of an implementation:
List<?> someListImplementation = geListMagically();
System.out.println(someListImplementation.getClass().getName());
Here's trivial example why we need getClass()
on java.lang.Object
:
Object obj = "A String";
System.out.println(obj.getClass().getName()); // calls Object#getClass()
// and prints java.lang.String :)

- 113,398
- 19
- 180
- 268
You may use to :
- get the constructors of the class :
Constructor[] constructors = this.getClass().getConstructors()
- get the fields of the class (reflection):
Field[] fields = this.getClass().getFields();
- use it instead of instanceof in equals method to perserve the symetry contract of the equal method (as explained here )
if (obj == null) return false; if (getClass() != obj.getClass()) return false;

- 1
- 1

- 38,870
- 10
- 48
- 69