Hmm, it seems we're just not connecting here.
Pity
I don't care what classes instantiate my class. What I want to do is from within *my* class, get the name of whatever class it was instantiated *from*.
Classes do not instantiate other classes
I want Bar to discover that your class is named Bob.
Bar can only do that inside Bob by making notorious use of an interface. You force all classes that are going to instantiate your class(Bar) to implement the InstanitiatesBar interface
Kids, do not try this at home. - interface InstantiatesBar {
-
}
-
class Bar {
-
InstantiatesBar object;
-
Bar(InstantiatesBar object) { //constructor requires an InstantiatesBar object
-
this.object = object;
-
-
}
-
}
-
class Bob implements InstantiatesBar {
-
Bob() {
-
Bar bar = new Bar(this);
-
System.out.println(bar.object.getClass());
-
}
-
public static void main(String ... args) {
-
new Bob();
-
}
-
}
-
Running that should print class Bob
There is a huge flaw in that code
Say some one writes another class
- class Lier implements InstantiatesBar {
-
Lier() {
-
Bob bob = new Bob();
-
Bar bar = new Bar(bob);
-
System.out.println(bar.object.getClass());
-
}
-
public static void main(String ... args) {
-
new Lier();
-
}
-
and you can guess the output to that.