Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
467 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

If you just want the name of the class, you could use this.getClass().getName().

Lastly, there are also this.getClass().getSimpleName() and this.getClass().getCanonicalName(). I use the former all the time to print readable class names to log files and the like.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...