• 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

"I'm super(), you're (blank)?"

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 classes- say ClassOne, ClassTwo, & ClassThree, extend SuperClass() and inherit the following method:
/* Returns the fully qualified name of the current class that called this method */
public String myNameIs()
{ ... }
So say I'm in ClassThree's main() and I say:
void main(...)
{
System.out.println(myNameIs());
}
I want it to say "ClassThree" and if I'm in ClassOne I want it to say "ClassOne". I do not want it to say "SuperClass" for any case. The purpose for this is not demonstrated in the example, because cleary I wouldn't have to do something stupid like that to get the name of the class I'm currently in.
From within a super class, can I determine the name of the child class that invoked one of the super class' methods?
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. Hopefully this is robust enough. I haven't fully tested it out, but it seems to work.If you really wanted "just" the classname (and not the fully qualified classname) you can use a few String methods to snip off everything in front of the last period character, and return just the last bit.
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hot dang! Thanks Mike!

outputs:
Naming.OneChild
Naming.SuperClass
MyNameIs() returns 2 different things depending on if SuperClass is extended or not, which seems ambiguous to me. Is there a way that I can explicitly say
-"Gimme the name of the class that extends me, null if there is none."
-"Gimme the name of my class, regardless if I'm extended or not"
?
[ March 22, 2002: Message edited by: David Duran ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, there is no way to find subclasses of a class with just reflection (using the getClass() methods and stuff).
One way that I have found to do this (if it's only your own classes that you want to find subclasses of) is to statically store the names of subclasses in their superclass. (complicated it sounds, no?)
Ok - In the super class, make a static variable that is a Vector or array of class names, or java.lang.reflect.Class objects. In each subclass, call that method in a static initializer, adding it as a subclass.
Ex:
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,
You said:


MyNameIs() returns 2 different things depending on if SuperClass is extended or not, which seems ambiguous to me.



It's not ambigous at all, since we're not querying an instance of SuperClass when we say one.MyNameIs(); We are asking 'one' who it is, and it properly responds "i am one".

It doesn't return two different things depending on whether or not we've extended the SuperClass. It's because we've asked two different instances who they are.

If your real requirement is actually what you ask here:

Is there a way that I can explicitly say
-"Gimme the name of the class that extends me, null if there is none."

Then I think using a mechanism like Adam's is the only way. "me" in your case is always the SuperClass, but the class has no idea WHO has extended it, unless we tell it to keep track of that. A child knows who its parent class is because of the keyword extends, but a parent class does not track its children. If this were built into the language, it would probably have to be a new keyword, something like sires or perhaps isParentOf
p.s.- Your second gimme:

"Gimme the name of my class, regardless if I'm extended or not"

is what the forName() method gives you. It gives the name of 'my class' ie: me, myself and I, what is my name, I don't care if I'm a bastard child or not.

p.p.s- Your code posted won't run. sc isn't declared before you try to 'out' it.
produces:Maybe *this* is what you meant? Here we see that a base class reference, that is holding a child class, can still properly resolve the eternal question: WhoAmI?
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, thank you guys. Your feedback was most helpful. By ambiguous I meant that if I look at the code in SuperClass() I would naturally think that it would return the name of itself and not the name of another class if it was extended. Maybe that's just a bad assumption.
FYI, this is how I stripped the fully qualified class name to just the class name:
reply
    Bookmark Topic Watch Topic
  • New Topic