5

My understanding is that java.lang.Class is the "entry point for all reflection operations". I also understand that when you instantiate a new object it will load the object's class if it's the first time it's needed.

Something something = new Something() 

Does calling Object.getClass(), or in our case something.getclass(), use reflection or is it just the methods inside Class itself that use reflection?

I would think that something.getClass() does not use reflection due to the fact that the Class's reference has been loaded and getClass would just be a getter for this reference, but I just want to make sure.

5
  • 2
    Possible duplicate of Difference between RTTI and reflection in Java Commented Jun 10, 2018 at 20:39
  • Don't understand what you mean by "use reflection." Asking for the class of an object is reflection. Commented Jun 11, 2018 at 0:25
  • I don't think this is a duplicate of "Difference between RTTI and reflection in java" since my question isn't related to instanceof but rather Object.getClass(). @jameslarge I think that is what I was trying to figure out: If asking for the class from an object used reflection. Since an instance of the class has already been created when the object was first instantiated, did calling getClass just return the reference to the already created Class or if expensive reflection operations were taking place. Commented Jun 11, 2018 at 1:14
  • OK, but you said, "use reflection" again. What does that mean? Why does it matter what it means? One person might say that asking for the class of an object is an example of "reflection." Another person might say, "but it doesn't use the java.lang.reflect API But so what? It does the same thing in either case. What is it that you are really trying to ask? Commented Jun 11, 2018 at 1:18
  • Ah, I see where you're coming from. Operations that use reflection (such as inspecting details about a class at runtime) are slow. I was going to create a HashMap<Class, Object> where the key is the object's class such that the collection can only have one entry per class type (i.e. there can only be one String, one Integer, etc...). Knowing that reflection (these runtime operations to identify something about a type) is slow, I didn't want to do this if calling obj.getClass() often to populate and work with with the map's keys would be "slower". Commented Jun 11, 2018 at 1:33

2 Answers 2

7

Checking the definitive source for Java's definition of reflection, the Java Language Specification, section 1.4. Relationship to Predefined Classes and Interfaces, simply says:

Consequently, this specification does not describe reflection in any detail. Many linguistic constructs have analogs in the Core Reflection API (java.lang.reflect) and the Language Model API (javax.lang.model), but these are generally not discussed here.

The Java Virtual Machine Specification, section 2.12. Class Libraries, says:

Classes that might require special support from the Java Virtual Machine include those that support:

  • Reflection, such as the classes in the package java.lang.reflect and the class Class.

So, you use reflection if you:

Note that the Class object returned by getClass() doesn't actually exist until you call it the first time. Classes are internally stored in a different way, and operations like instanceof function without having to instantiate a much heavier Class object, for performance reasons.

Sign up to request clarification or add additional context in comments.

Comments

3

According to the definition in Wikipedia:

In computer science, reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.

From this point of view, Object.getClass() is reflection per se since it can be used to examine the given object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.