28

I had a discussion with a coworker today, whether usage of using the Java operator instanceof is a kind of reflection. And the discussion quickly evolved into what actually defines reflection.

So, what is the definition of reflection?

And is the usage of instanceof considered "using reflection" ?

And in addition, if instanceof is considered reflection, then is polymorphism not also "using reflection"? If not, what is the difference?

Bjarke Freund-Hansen
  • 1,176
  • 1
  • 11
  • 18
  • 4
    You've pretty much answered your own question. How you define 'reflection' determines whether `instanceof` is an example of reflection. Certainly it is somewhere between normal data use and metadata use via `getClass()` & friends, but you can have workable definitions wither way. – Kilian Foth Sep 07 '11 at 07:26
  • Is the usage of `return` considered "structural programming"? – SF. Sep 07 '11 at 09:13

5 Answers5

24

This is the definition of reflection according to wikipedia:

In computer science, reflection is the process by which a computer program can observe (do type introspection) and modify its own structure and behavior at runtime.

I couldn't have said it better myself and highlighted the important part for your question. That said, yes, instanceof is considered using reflection. The program observes its structure and conducts type introspection.

Falcon
  • 19,248
  • 4
  • 78
  • 93
  • Can getting the type of a variable be considered 'introspection'? :) A simple `if ( true )` also 'observes' the true value. – Steven Jeuris Sep 07 '11 at 07:40
  • I see the wikipedia page on type introspection confirms your statement. Thinking about it, actually thinking of `instanceof` as reflection is a good thing. When it is not used for reflection purposes it usually indicates a code smell. – Steven Jeuris Sep 07 '11 at 07:47
  • Your example can't be considered introspection. It's just a value being checked and compared. The type only matters to the compiler (and not even in all languages). Your little program isn't observing itself and its types. "Can getting the type of a variable be considered 'introspection'?" Yes, this can be considered introspection. – Falcon Sep 07 '11 at 07:48
  • 3
    @Steven Jeuris: The difference is that `if (true)` looks at a *value*, not at a *type*. That's why it's not considered reflection. – sleske Sep 07 '11 at 07:50
  • @Steven Jeuris: `if` statement is a flow control statement. Sure it observers value, but takes immediate action after observation is complete. And you can't do anything about what happens after observation is complete. This isn't reflection in my book. – Jacek Prucia Sep 07 '11 at 07:56
  • 1
    @sleske: The reason why I mentioned it was because observing a type is such a rudimentary principle. Without it there would be no polymorphism. Technically speaking this means a simple override could also be considered reflection (or at least results in). Do not forget the second part of the definition: **and modify its own structure and behavior at runtime.** All in all I upvoted this answer (check my second comment), but I do understand where the question from the OP came from. ;p – Steven Jeuris Sep 07 '11 at 08:06
  • 1
    According to that definition of reflection, would normal polymorphism not also be a kind of reflection? – Bjarke Freund-Hansen Sep 07 '11 at 08:07
  • @Jacek: You can't do much more after **just** using `instanceof` either, it also just controls flow. – Steven Jeuris Sep 07 '11 at 08:07
  • 4
    @bjarkef: I would say "no", as the virtual dispatch used by polymorphism is done implicitly by the language itself, rather than the application code explicitly examining the object in question to identify its characteristics. – Dave Sherohman Sep 07 '11 at 08:31
  • 2
    @Dave Sherohman: I'd also add that most of the work (setting up jump tables, etc) is done at compile time, not run time. – TMN Sep 07 '11 at 17:00
14

For the sake of clarity, I would consider two answers.

Theoretically, instanceof is a form of reflection, as explained in Falcon's answer.

In computer science, reflection is the process by which a computer program can observe (do type introspection) and modify its own structure and behavior at runtime.

However, practically, when a programmer talks about using reflection he usually refers to much more than just checking whether a variable is of a certain type. This is such a rudimentary concept, without which, polymorphism wouldn't be possible.

Do note that using instanceof often indicates a code smell, and proper polymorphism can often be used instead.

Steven Jeuris
  • 5,804
  • 1
  • 30
  • 52
3

Since the other answers, the definition in Wikipedia has been changed (I happen to agree with this change, but I wasn't the one who made it) to remove the part in parentheses:

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

https://en.wikipedia.org/wiki/Type_introspection also specifically says

Introspection should not be confused with reflection

I wouldn't consider instanceof to fall under the first definition: the program examines a value, not its own structure, and it doesn't involve any values representing program structure (in Java: java.lang.Class, java.reflect.Method, etc.); the hallmark of reflection is working with such values.

Alexey Romanov
  • 1,467
  • 11
  • 15
2

You might be interesting in the following article: http://java.sun.com/developer/technicalArticles/ALT/Reflection/

The key in this article is a code snippet where they simulate the "instanceof" keyword by using the "isInstance" method of the "Class" class, which is part of the reflection feature of Java.

Jalayn
  • 9,789
  • 4
  • 39
  • 58
-5

YES, "instanceof" operator its a form of reflection. The previous answers tell you why.

"Reflection" or "object / class introspection" is a hype these days, but several programming languages have use some of that concept, from some time.

DOT NET (Java clone), (mis) uses a lot.

umlcat
  • 2,146
  • 11
  • 16