Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - get the current class name?

All I am trying to do is to get the current class name, and java appends a useless non-sense $1 to the end of my class name. How can I get rid of it and only return the actual class name?

String className = this.getClass().getName(); 
like image 581
aryaxt Avatar asked Jun 07 '11 20:06

aryaxt


People also ask

How do you return a class name in Java?

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 class name of an object?

If you have a JavaSW object, you can obtain it's class object by calling getClass() on the object. To determine a String representation of the name of the class, you can call getName() on the class.

What is getClass () getName () in Java?

The getName() method of java Class class is used to get the name of the entity, and that entity can be class, interface, array, enum, method, etc. of the class object. Element Type.


1 Answers

Try,

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

This will work as long as you don't use it in a static method.

like image 189
jesg Avatar answered Sep 18 '22 19:09

jesg