Sunday, March 3, 2024

Generating Getters And Setters Using Reflection in Java

When you right click on any Java Bean class name with in the eclipse IDE and click on Source – Generate Getters and Setters you get the getter and setter methods for the selected fields. Ever wondered what goes in the background to generate the getter and setter methods programmatically?

Yes, it is the magic of reflection in Java which gets the information about the fields of the class and their types and then generate the getters and setters using reflection accordingly.

If you have to create such a functionality yourself then using the Java Reflection API you can create your own getters and setters generator class, of course just for academic purpose, as all the IDEs anyway provide the facility to do that.

Getters & Setters generator using reflection example

Let’s say you have a class TestClass with three fields of type int, String and Boolean and you want to generate getters and setters for these 3 fields using Java reflection API.


public class TestClass {
 private int value;
 private String name;
 private boolean flag;

GetterSetterGenerator class

Using the Reflection API for the field you can get information about the fields of the given class – like name and type. Once you have that information you can create set and get methods for the fields. In this code set and get methods are just printed after creating them as it is just for illustrating the usage of reflection in Java so not going into File I/O.

import java.lang.reflect.Field;
import java.util.Arrays;

public class GetterSetterGenerator {

 public static void main(String[] args) {
  try {
   GetterSetterGenerator gt = new GetterSetterGenerator();
   StringBuffer sb = new StringBuffer();
   Class<?> c = Class.forName("org.prgm.TestClass");
   // Getting fields of the class
   Field[] fields = c.getDeclaredFields();
   System.out.println("Fields - " + Arrays.toString(fields));
   for(Field f : fields){
    String fieldName = f.getName();
    String fieldType = f.getType().getSimpleName();
    System.out.println("Field Name -- " + fieldName);
    System.out.println("Field Type " + fieldType);
    gt.createSetter(fieldName, fieldType, sb);    
    gt.createGetter(fieldName, fieldType, sb);
   System.out.println("" + sb.toString());
  }catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block

 private void createSetter(String fieldName, String fieldType, StringBuffer setter){
  setter.append("public void").append(" set");
  setter.append("(" + fieldType + " " + fieldName + ") {");
  setter.append("\n\t this."+ fieldName + " = " + fieldName + ";");
  setter.append("\n" + "}" + "\n");
 private void createGetter(String fieldName, String fieldType, StringBuffer getter){
  // for boolean field method starts with "is" otherwise with "get"
  getter.append("public " + fieldType).append((fieldType.equals("boolean")?" 
    is" : " get") + getFieldName(fieldName) + "(){");
  getter.append("\n\treturn " + fieldName + ";");
  getter.append("\n" + "}" + "\n");
 private String getFieldName(String fieldName){
  return fieldName.substring(0, 1).toUpperCase() + fieldName.substring(
    1, fieldName.length());


Fields - [private int org.prgm.TestClass.value, private java.lang.String, private boolean org.prgm.TestClass.flag]
Field Name -- value
Field Type int
Field Name -- name
Field Type String
Field Name -- flag
Field Type Boolean

public void setValue(int value) {
  this.value = value;
public int getValue(){
 return value;
public void setName(String name) { = name;
public String getName(){
 return name;
public void setFlag(boolean flag) {
  this.flag = flag;
public boolean isFlag(){
 return flag;

That's all for this topic Generating Getters And Setters Using Reflection in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page

Related Topics

  1. Invoking Getters And Setters Using Reflection in Java
  2. Reflection in Java - Getting Class Information
  3. Reflection in Java - Getting Method Information
  4. How to Pass Command Line Arguments in Eclipse
  5. How to Compile Java Program at Runtime

You may also like-

  1. Remove Duplicate Elements From an Array in Java
  2. Format Date in Java Using SimpleDateFormat
  3. Read or List All Files in a Folder in Java
  4. Nested class and Inner class in Java
  5. Externalizable interface in Java
  6. Constructor Chaining in Java
  7. Parallel Stream in Java Stream API
  8. Method Reference in Java