4

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.

Programmer001
  • 2,240
  • 2
  • 19
  • 35
  • 2
    Possible duplicate of [Difference between RTTI and reflection in Java](https://stackoverflow.com/questions/16553836/difference-between-rtti-and-reflection-in-java) – John Jun 10 '18 at 20:39
  • Don't understand what you mean by "use reflection." Asking for the class of an object _is_ reflection. – Solomon Slow Jun 11 '18 at 00: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. – Programmer001 Jun 11 '18 at 01: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](https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/package-summary.html) But so what? It does the same thing in either case. What is it that you are really trying to ask? – Solomon Slow Jun 11 '18 at 01: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 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". – Programmer001 Jun 11 '18 at 01:33

2 Answers2

6

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 difference way, and operations like instanceof functions without having to instantiate the much heavier Class object, for performance reasons.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
4

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221