Sunday, July 16, 2017

The difference between a.getClass() and A.class in Java

  • 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.
@reference_1_stackoverflow
What is the difference between a.getClass() and A.class in Java?
@reference_2_docs.oracle.com
Retrieving Class Objects

@reference_3_coderanch.com
Overriding Vs Hiding
Foo f = new Bar();
f.getClass().getMethod("classMethod", new Class[]).invoke(null, new Object[]);

No comments:

Post a Comment