1. Overview

In our previous article, we discussed how we could read the values of private fields from a different class in Java. However, there can be scenarios when we need to set the values of fields, such as in some libraries where we don’t have access to the fields.

In this quick tutorial, we’ll discuss how can we set the values of fields from a different class in Java by using the Reflection API.

Note that we’ll be using the same Person class for the examples here as we used in our previous article.

2. Setting Primitive Fields

We can** set the fields that are primitives by using the Field#setXxx methods**.

#java #reflection

Set Field Value With Reflection
1.45 GEEK