ycomp
ycomp

Reputation: 8573

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

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

Upvotes: 7

Views: 15646

Answers (2)

Sunil Kumar B M
Sunil Kumar B M

Reputation: 2795

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

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

Upvotes: 3

Sam
Sam

Reputation: 6890

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.

Upvotes: 18

Related Questions