• 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

Can superclass object find the subclass type that is the true type of instance?

 
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say I have a class B that extends A with the constructor:

Is there a way to have something like the following in A?

where it would print "B" since that is the actual class, not A.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:



Class B isn't public because I didn't bother to put it in a separate file. If you do, then it should be.

Ryan
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, now I'm guessing that this only works on an instance level, correct?

So if I have a static section in class A, then when class B is loaded, it would NOT also invoke the static section in A, right?

 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When B is loaded, its superclass A is loaded first. So yes, the static section is invoked.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's usually only necessary to load a class once in a given JVM. So it's possible that executing code will refer to class A, causeing it to be loaded, and then later on the code refers to B, causing it to be loaded. Since A was already loaded (by itself), it will not be reloaded just before B is loaded. So: you can guarantee that a static initializer in A will run sometime before a static initializer in B - but it's hard to say exactly when. (Unless you follow exactly what code is getting executed.) And when A is loaded the first time, you have no real way of knowing if it's getting loaded just because B is getting loaded, or for some other reason. So there's not really an equivalent of getClass() that you can use to figure out what's going on.
[ May 10, 2005: Message edited by: Jim Yingst ]
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic