Tuesday, May 4, 2010

How do I – Eclipse: generate getter/setter for class fields

Its a good practice to keep your class fields private. But declaring getter/setter for each field may be a time consuming (and boring) operation, for instance, if you have more than 3 private class fields.

But, Eclipse has a nice helper mechanism of generating getters/setters automatically from your class fields.

Lets say we have class Animal with 4 fields and we want to generate getters (and setters) for them:

import android.graphics.Color;

public class Animal {

      private int age;

      private String kind;

      private int weight;

      private Color color;


Open context menu and select “Source” –> “Generate Getters and Setters…”.


You will get the following dialog:


Configure your fields and press OK and you will get your getters/setters generated for you.

Happy Eclipse’ing

No comments :

Post a Comment