Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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 7 '11 at 7:26
    
Is the usage of return considered "structural programming"? –  SF. Sep 7 '11 at 9:13
add comment

4 Answers

up vote 13 down vote accepted

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.

share|improve this answer
    
Can getting the type of a variable be considered 'introspection'? :) A simple if ( true ) also 'observes' the true value. –  Steven Jeuris Sep 7 '11 at 7:40
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 7 '11 at 7:50
1  
According to that definition of reflection, would normal polymorphism not also be a kind of reflection? –  bjarkef Sep 7 '11 at 8:07
3  
@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 7 '11 at 8:31
1  
@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 7 '11 at 17:00
show 5 more comments

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.

share|improve this answer
    
+1 for the potential code smell - not always, just the potential –  Gary Rowe Sep 8 '11 at 9:33
add comment

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.

share|improve this answer
    
Nice explanation for Java. –  Falcon Sep 7 '11 at 7:49
add comment

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.

share|improve this answer
1  
Uuuuhm, … dude? –  Konrad Rudolph Sep 7 '11 at 16:20
    
Your first sentence is correct, and redundant in the face of previous answers. The rest is just wrong. –  Frank Shearar Sep 8 '11 at 8:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.