What’s Java Reflection and How Does it Work?

What's Java Reflection and How Does it Work?

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

Are you pursuing a programming career with Java? Interviews concerning Java can vary from technical and non-technical. In this article, we shall look at one of the most common technical java questions; What is Java Reflection and How Does it Work? Therefore, you can provide these answers in your interview.

What reflection in Java Means

Java reflection is a way through which a programmer can inspect classes, interfaces, fields and methods during runtime without knowing their respective names during the compile time. Reflection also makes it possible to instantiate, refer or implore methods and get the set field values. That is one way you can answer this question.

Additionally, you can go ahead to mention the various applications of the reflection in Java. When developing a program in Java, reflection can be used to map objects to specific tables during runtime, just like the butterfly persistence function. Also, you can use it when allocating a statement in a scripting language to a method call on actual objects during runtime, just like what the butterfly container function does.

How Does Java Reflection Work?

When used correctly, the reflection function can help you query for specific classes by their particular names. An example of a reflection function can be as follows:

Class stringClass = String.class;
Method toLCMethod = stringClass.getMethod(“toUpperCase”);
System.out.println(toLCMethod.invoke(“Good, morning!”));

The above calling instance method shows a contrived usage of reflection.

Java is the only language where it is possible to obtain the functions defined within a program. In JavaBeans, reflection allows components to be operated on visually through a builder tool. The tool utilizes the reflection to get the properties of Java classes as they get loaded dynamically.

Where Can You Find Reflection Classes in Java?

The reflection classes (method for instance) when setting up the reflection function are usually found in java.lang.reflect. Three steps exist that have to be followed to be able to utilize the classes:

  • Obtaining the ?java.lang.class? object for the class on which you want to operate. This function represents the classes and phases in the runtime.
  • Querying a method like getDeclareMethods to give you an outline of all the methods called by the class.
  • Using the reflection API to operate on the information.

However, when used incorrectly there may be issues such as the following output:?java.lang.reflect.invocationtargetexception

Invocation target exception may mean that in your reflection code, the method that you queried resulted into an exception. It can be solved by looking for the problem in the main method itself through wrapping the method query call around a try-catch block and log functions. For more information on how to use the reflection function, you can visit here.


Since Java is the only programming language that supports the operations of the reflection function, it is important that you prove to your interviewer that you know how it operates. It is a superior tool that allows programmers identify problems within the methods as well as class identification. You can air out your thoughts on this review or add onto it more information.

Recent Posts