File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes getClass( ) method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getClass( ) method" Watch "getClass( ) method" New topic

getClass( ) method

mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
what is the use of getClass( ) method?
can the runtime class of the object be different than the class inwhich it is created?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Gives you the ACTUAL class of the object instance, as opposed to the referenent type (the class of the variable). For example:

Although the variable "conn" is declared to be of type "Connection", we know that "Connection" is an interface, and interfaces can't be instantiated. So you can invoke the "getClass()" method to return the actual class of the variable "conn".
If you look in java.lang.Class, you'll see there are methods that are can be invoked on the "class", and one way to get the class is to call the "getClass()" method.
Another quick example (beware, BAD CODING AHEAD ...):

Since everything (other than a primitive) is ultimately descended from Object, we can any of that into an "Object" reference. But we don't have three "java.util.Object" instances; we have a java.lang.String, a java.awt.Point, and a java.util.ArrayList. Using "myStuff[0].getClass()" will tell you the actual class of what is being referenced.
[ November 14, 2003: Message edited by: Wayne L Johnson ]
Jian Zhang

Joined: Aug 30, 2003
Posts: 20
I don't mean to argue, but i think the elements in an array should hold the same type,is that right?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
All the elements of the array have to match the declaration. In that example it was declared Object[] and all the elements were indeed Objects.
I'd guess most examples that have different classes in an array have some kind of polymorphism in mind. Like an array declared Shape[] might have Circle, Rectangle, Triangle, IrregularPolygon or whatever, and you might ask each one to getArea(). Would that make sense to you?
But there are no rules. With Object[] you could put just about anything except primitives if you had a reason to. I agree with you that I can't think of a good reason to right off, tho.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You are right: as a rule you always want to put the same type of objects into an Array. The example I gave was deliberately poor programming, which is why I included the disclaimer, "(beware, BAD CODING AHEAD ...):".
I was not trying to show how one should program, but simply trying to answer the question posed, showing the different between referent type (Object) and actual instance type (String, Point and ArrayList).
The referent type is used at compile time to verify that the given object implements the referenced message. The actual object instance type is used at run time to resolve the method to the appropriate class.
Maybe I should have used an ArrayList or Vector instead of an Array. With ArrayLists (and other collections) you dump in "Object" instances and all you get back are "Object"s. To call meaningful methods on them you must cast them back to the desired type.
Mrudul's original question was whether the runtime class of an object can be different from the created class. The answer is a qualified "yes". If you say:
Object myObj = new String("abc def");
To the compiler "myObj" is a "java.lang.Object" type, and the only methods you can invoke on it are those in "Object". However at runtime the JVM will correctly recognize that "myObj" is actually a "java.lang.String". The referent type is "java.lang.Object", but the actual object instance is "java.lang.String".
I think I've officially beat this dead horse ...
I agree. Here's the link:
subject: getClass( ) method
It's not a secret anymore!