say there are 2 classes...
public class NotAbstract1 extends AnAbstract {
NotAbstract1() { super(); }
}
public class NotAbstract2 extends AnAbstract {
NotAbstract2() { super(); }
}
public abstract class AnAbstract {
AnAbstract() { //do something }
abstract void saySomething() { System.out.println("Something"); }
}
In this example, NotAbstract1 and NotAbstract2 can call saySomething(). How can I, from within the saySomething() method of AnAbstract, recognize the class which called it? Without passing in the class or an identifier.
Again, the easy solution is to change the method signature to be saySomething(Class clazz) but I'd like to not do that. I have a feeling it can be done with reflection so I'm adding that tag to the question.
You can call
this.getClass()atsaySomething()- it will return you the type of the current instance - eithernotAbstract1ornotAbstract2.