Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is reflection and why is it useful?

What is reflection, and why is it useful?

I'm particularly interested in Java, but I assume the principles are the same in any language.

like image 402
Lehane Avatar asked Sep 01 '08 08:09

Lehane


People also ask

Why are reflections useful?

The most useful reflection involves the conscious consideration and analysis of beliefs and actions for the purpose of learning. Reflection gives the brain an opportunity to pause amidst the chaos, untangle and sort through observations and experiences, consider multiple possible interpretations, and create meaning.

What is reflection and why is it useful in Java?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.

What is reflection and how is it used?

Reflection is an API which is used to examine or modify the behaviour of methods, classes, interfaces at runtime. The required classes for reflection are provided under java. lang.

What is reflection and why is it important in nursing?

Reflection allows you to make sense of a situation and understand how it has affected you. It allows you to identify areas for learning and development to include in your professional development objectives and supports sharing and learning from other professionals.


1 Answers

The name reflection is used to describe code which is able to inspect other code in the same system (or itself).

For example, say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to.

So, to give you a code example of this in Java (imagine the object in question is foo) :

Method method = foo.getClass().getMethod("doSomething", null); method.invoke(foo, null); 

One very common use case in Java is the usage with annotations. JUnit 4, for example, will use reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test.

There are some good reflection examples to get you started at http://docs.oracle.com/javase/tutorial/reflect/index.html

And finally, yes, the concepts are pretty much similar in other statically typed languages which support reflection (like C#). In dynamically typed languages, the use case described above is less necessary (since the compiler will allow any method to be called on any object, failing at runtime if it does not exist), but the second case of looking for methods which are marked or work in a certain way is still common.

Update from a comment:

The ability to inspect the code in the system and see object types is not reflection, but rather Type Introspection. Reflection is then the ability to make modifications at runtime by making use of introspection. The distinction is necessary here as some languages support introspection, but do not support reflection. One such example is C++

like image 143
Matt Sheppard Avatar answered Oct 10 '22 06:10

Matt Sheppard