IAmYourFaja - 1 year ago 61
Java Question

# What is the difference between a.getClass() and A.class in Java?

In Java what pros/cons exist surrounding the choice to use

a.getClass()
or
A.class
? Either can be used wherever a
Class<?>
is expected, but I imagine that there would be performance or other subtle benefits to using both in different circumstances (just like there are with
Class.forName()
and
ClassLoader.loadClass()
.

• a.getClass() returns the runtime type of a. I.e., if you have A a = new B(); then a.getClass() will return the B class.
• A.class evaluates to the A class statically, and is used for other purposes often related to reflection.