• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getClass( ) method

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the use of getClass( ) method?
can the runtime class of the object be different than the class inwhich it is created?
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't mean to argue, but i think the elements in an array should hold the same type,is that right?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic