@Getter and @Setter

Overview

You can annotate any field with @Getter and/or @Setter, to let lombok generate the default getter/setter automatically.
A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type is boolean). A default setter is named setFoo if the field is called foo, returns void, and takes 1 parameter of the same type as the field. It simply sets the field to this value.

The generated getter/setter method will be public unless you explicitly specify an AccessLevel, as shown in the example below. Legal access levels are PUBLIC, PROTECTED, PACKAGE, and PRIVATE.

You can also put a @Getter and/or @Setter annotation on a class. In that case, it's as if you annotate all the non-static fields in that class with the annotation.

You can always manually disable getter/setter generation for any field by using the special AccessLevel.NONE access level. This lets you override the behaviour of a @Getter, @Setter or @Data annotation on a class.

With Lombok

01 import lombok.AccessLevel;
02 import lombok.Getter;
03 import lombok.Setter;
04 
05 public class GetterSetterExample {
06   @Getter @Setter private int age = 10;
07   @Setter(AccessLevel.PROTECTEDprivate String name;
08   
09   @Override public String toString() {
10     return String.format("%s (age: %d)", name, age);
11   }
12 }

Vanilla Java

01 public class GetterSetterExample {
02   private int age = 10;
03   private String name;
04   
05   @Override public String toString() {
06     return String.format("%s (age: %d)", name, age);
07   }
08   
09   public int getAge() {
10     return age;
11   }
12   
13   public void setAge(int age) {
14     this.age = age;
15   }
16   
17   protected void setName(String name) {
18     this.name = name;
19   }
20 }

Small print

For generating the method names, the first character of the field, if it is a lowercase character, is title-cased, otherwise, it is left unmodified. Then, get/set/is is prefixed.

No method is generated if any method already exists with the same name, even if the parameter list is different. For example, getFoo() will not be generated if there's already a method getFoo(int x) even though it is technically possible to make the method. This caveat exists to prevent confusion. If the generation of a method is skipped for this reason, a warning is emitted instead.

For boolean fields that start with is or has immediately followed by a title-case letter, nothing is prefixed to generate the getter name.

Any variation on boolean will not result in using the is prefix instead of the get prefix; for example, returning java.lang.Boolean results in a get prefix, not an is prefix.

Any annotations named @NonNull (case insensitive) on the field are interpreted as: This field must not ever hold null. Therefore, these annotations result in an explicit null check in the generated setter. Also, these annotations (as well as any annotation named @Nullable or @CheckForNull) are copied to setter parameter and getter method

Using the AccessLevel.NONE access level simply generates nothing. It's useful only in combination with @Data.