Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the classname at runtime, but only the classname?

Tags:

java

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

like image 431
ycomp Avatar asked Feb 05 '12 08:02

ycomp


People also ask

How do I get a classname class?

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.

How do I find the classname in Testng?

If you want to get super class name you can use getSuperClass().

How do you get a classname in typescript?

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.

How can you get access to the current instance of the class that code is running?

There are two ways to access the instance variable of class:Within the class by using self and object reference. Using getattr() method.


2 Answers

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.

like image 71
Sam Avatar answered Nov 15 '22 20:11

Sam


String className = object.getClass().getSimpleName(); 

http://www.coderanch.com/t/410851/java/java/Class-Name

like image 25
Sunil Kumar B M Avatar answered Nov 15 '22 20:11

Sunil Kumar B M