How can I get the classname at runtime, but only the actual classname and not the whole "com.xyz.etc." ?
I mean only the part of the name after the last period
The simplest way is to call the getClass() method that returns the class's name or interface represented by an object that is not an array. We can also use getSimpleName() or getCanonicalName() , which returns the simple name (as in source code) and canonical name of the underlying class, respectively.
If you want to get super class name you can use getSuperClass().
Use the name property on an object's constructor to get the class name of the object, e.g. const className = e1.constructor.name . The constructor property returns a reference to the constructor function that created the object.
There are two ways to access the instance variable of class:Within the class by using self and object reference. Using getattr() method.
You have to use this snippet code for object:
yourObject.getClass().getSimpleName();
or for class use :
yourClass.class.getSimpleName();
this code return only name of class, does not consist package name.
String className = object.getClass().getSimpleName();
http://www.coderanch.com/t/410851/java/java/Class-Name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With