blog;

Java Reflection.

What is reflection in Java?

Java Reflection.

Originally posted on Fri Sep 17 2021

Recently while having lunch with the more senior developers, the term Java Reflection was brought up quite a bit in a discussion regarding the pros and cons of Java versus other languages. It was something I had not really heard of before, so I made a note to look into what that actually meant.

Reflection as it turns out is something that a few languages support, and while as I mainly work with Java I will focus on Java Reflection, hopefully the idea behind it is applicable to other languages.

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.
Source: Oracle

So for example, say you have an Object with an unknown type, and you want to call a method doStuff() if it exists. Java isn't really designed to handle this, however with Java reflection, the code can look inside the Object and find out if it has the method, then call it if it exists.

Method method = object.getClass().getMethod("doStuff", null);
method.invoke(object, null);

The practical use of this is when you have an object and you don't know it's type, but you want to know more about it.

A simple example showing this...

Let's create a simple Car class with no methods.

public class Car {
    private String brand;
    private int doors;
    private int seats;
}

Now using Java reflection we can discover all of the fields of the object:

Object car = new Car();
Field[] fields = car.getClass().getDeclaredFields();

List<String> fieldNames = new ArrayList<>();
for (Field field : fields)
    fieldNames.add(field.getName());
return fieldNames;

fieldNames == List.of("brand", "doors", "seats")

Uses

Extensibility Features

An application can make use of external, user-defined classes by extending existing classes.

Tests and Debugging

Test engines may use the @Test annotation to tag tests, then use reflection to find them in the code where running the tests. Debuggers may need to be able to examine private members of a class.

Development Environments

IDE's can make use of type information available in reflection to aid in writing code.


Drawbacks

Generally, despite its uses, if you can do something without reflection, you should.

Performance

Reflection is powerful but it stops the JVM optimising the code as much as it could without reflection. This means it performs a lot slower than non-reflective code.

Exposure of Internals

Through reflection, you can perform some operations that you shouldn't be able to, such as accessing private variables. Reflective code also breaks abstractions.

See more here: https://docs.oracle.com/javase/tutorial/reflect/index.html

Java Reflection
Java Reflection
Source: My incredible artistic talent

Me

Post by

Josh Glasson

Software Developer. Creator and owner of this blog.