Reflection (computer science)

In computer science, reflection is the ability of a program to examine and possibly modify its high level structure at runtime. It is most common in high-level virtual machine programming languages like Smalltalk, and less common in low-level programming languages like C.

When program source code is compiled, information about the structure of the program is normally lost as lower level code (typically assembly language code) is produced. If a system supports reflection, the structure is preserved as metadata with the emitted code.

Though depending on the implementation, code with reflection tends to run slower than that without it.

Known platforms supporting reflection are: Forth, Lisp, Java Virtual Machine, .NET's Common Language Runtime, dynamically typed languages like Objective-C and Smalltalk, scripting languages like Perl, Python, PHP, Ruby and the Maude system of rewriting logic.

In languages that do not make distinction between run time and compile-time (Lisp and its variants, for example), there is no difference between compilation or interpretation of code and reflection.

More generally, reflection is an activity in computation that reasons about its own computation. The programming paradigm driven by reflection is called reflective programming.


A language supporting reflection provides a number of features available at runtime that would otherwise be very obscure or impossible to accomplish in a lower-level language. Some of these features include:

  • The ability to discover and modify source-code constructions (such as code blocks, classes, methods, protocols, etc.) as first-class objects at runtime.
  • The ability to convert a string matching the symbolic name of a class or function into an invocation of that class or function.
  • The ability to evaluate a string as if it were a source-code statement at runtime.

These features can be implemented in different ways. Interpreted programming languages, such as Ruby and PHP, are ideally suited to reflection, since their source code is never lost in the process of translation to machine language— the interpreter has the source readily available. Compiled languages rely on their runtime system to provide information about the source code. A compiled Objective-C executable, for example, records the names of all methods in a block of the executable, providing a table to correspond these with the underlying methods (or selectors for these methods) compiled into the program.


The following is an example in Java. Consider two pieces of code

// Without reflection
Foo foo = new Foo ();
foo.hello ();
// With reflection
Class cl = Class.forName ("Foo");
Method method = cl.getMethod ("hello", null);
method.invoke (cl.newInstance (), null);

Both code fragments create an instance of a class 'Foo' and call its 'hello' method. The difference is that, in the first fragment, the names of the class and method are hard-coded; it is not possible to use a class of another name. In the second fragment, the names of the class and method can vary at runtime.

(See java.lang.reflect for more of this feature.)

Here is an equivalent example in Perl:

# without reflection
# with reflection
my $class = "Foo";
my $method = "hello";


  1. Reflection in logic, functional and object-oriented programming: a Short Comparative Study ( (Citeseer page).de:Reflexion (Programmierung)

  • Art and Cultures
    • Art (
    • Architecture (
    • Cultures (
    • Music (
    • Musical Instruments (
  • Biographies (
  • Clipart (
  • Geography (
    • Countries of the World (
    • Maps (
    • Flags (
    • Continents (
  • History (
    • Ancient Civilizations (
    • Industrial Revolution (
    • Middle Ages (
    • Prehistory (
    • Renaissance (
    • Timelines (
    • United States (
    • Wars (
    • World History (
  • Human Body (
  • Mathematics (
  • Reference (
  • Science (
    • Animals (
    • Aviation (
    • Dinosaurs (
    • Earth (
    • Inventions (
    • Physical Science (
    • Plants (
    • Scientists (
  • Social Studies (
    • Anthropology (
    • Economics (
    • Government (
    • Religion (
    • Holidays (
  • Space and Astronomy
    • Solar System (
    • Planets (
  • Sports (
  • Timelines (
  • Weather (
  • US States (


  • Home Page (
  • Contact Us (

  • Clip Art (
Personal tools