Get concrete class name from abstract class

I apologize if this question has already been asked. I searched for questions and could not find the answer.

In Java, inside an abstract class, can I get an instance of a specific class that extends it?

If so, can I see some sample code?

+47
java abstract-class
Aug 01 '11 at 16:52
source share
1 answer

Yes, you can do this by calling this.getClass() . This will give you a Class instance for the runtime type this .

If you just need a class name, you can use this.getClass().getName() .

Finally, there is also this.getClass().getSimpleName() and this.getClass().getCanonicalName() . I use the former all the time to print readable class names for log files, etc.

+63
Aug 01 '11 at 16:53
source share



All Articles