Tuesday, September 18, 2012

How to Avoid Getter and Setter Methods in JAVA, JSF and any POJO's

The lombok java utility helps to avoid getter and setter methods in java, especially in JSF and any POJO's 

After Installing it (very simple 2 click process) and add the same jar to your project's classpath.

Here is an example of how to avoid getters and setters in java using lombak.jar , scala does that but just to avoid getters and setters this utility is more than enough.

public class Person{
public String name;
public int age;
public int getAge(){
   return this.age;
public void setAge(int argAge){
 this.age = argAge;
//[...] Other Getters and Setters HERE.

To convert the POJO use the "@Data" annotation as follows. 

@Data public class Person{
   public String name;
   public int age;
That's it deploy / code / refactor / life is short - Enjoy!

  • There are more handy and specific annotations head over to the website.
  • Reduces all the noisy clutter methods in the class, no need for the lombak jar to be present during runtime.
  • Works like magic with IBatis & JRebel also!
  • Your IDE still sees the getter and setter methods as though they exist. (Atleast in eclipse it does for me! )
  • Reduces the code size of the project by at least 30%. 

Many Thanks to the lombak team!