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?

asked Sep 7, 2011 at 7:23
2
  • 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. Commented Sep 7, 2011 at 7:26
  • Is the usage of return considered "structural programming"? Commented Sep 7, 2011 at 9:13

5 Answers 5

25

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 .

answered Sep 7, 2011 at 7:38
10
  • 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. Commented Sep 7, 2011 at 7:50
  • 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 Commented Sep 7, 2011 at 8:06
  • 1
    According to that definition of reflection, would normal polymorphism not also be a kind of reflection? Commented Sep 7, 2011 at 8: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. Commented Sep 7, 2011 at 8: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. Commented Sep 7, 2011 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.

answered Sep 7, 2011 at 8:17
1
  • 1
    +1 for the potential code smell - not always, just the potential Commented Sep 8, 2011 at 9:33
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.

answered Sep 14, 2016 at 14:54
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.

answered Sep 7, 2011 at 7:43
1
  • Nice explanation for Java. Commented Sep 7, 2011 at 7:49
-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.

answered Sep 7, 2011 at 15:57
2
  • 1
    Uuuuhm, ... dude? Commented Sep 7, 2011 at 16:20
  • 3
    Your first sentence is correct, and redundant in the face of previous answers. The rest is just wrong. Commented Sep 8, 2011 at 8:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.