Get child class name from parent Get child class name from parent android android

Get child class name from parent


On some occasions simply this line in the parent class solves this problem. It returns the name of the "child" class (not the parent):

this.getClass().getName() //String like "com.mycompany.myclassname"this.getClass().getSimpleName() //String like "myclassname"

See here for further discussion: http://www.coderanch.com/t/324715/java/java/Getting-child-class-name-parent


Use instanceof operator.

Supposing you have a base class and two subclasses named Base, SubOne and SubTwo, if you want to check if a variable ref is an instance of SubOne or SubTwo you'd say:

if(ref instanceof SubOne){}else if(ref instanceof SubTwo){}

Note that: (ref instanceof Base) will always return true though.


I think you want to use the instanceof operator, for example:

if(this instanceof SomeCustomActivity) {    // do stuff} else if (this instanceof AnotherCustomActivity) {    // do other stuff}

And that is all there is to it.