@Getter And @Setter

How To Use @Getter And @Setter Annotations With Project Lombok

@Getter and @Setter annotations enable you avoid having to manually write getter and setter methods in Java classes.

TIP: For those who would like to follow along with this blog, just clone my repository git clone https://github.com/steven7mwesigwa/java-tutorials.git and navigate to java-tutorials\project-lombok\getter-setter. In there, you should be able to play around with all the source code used in this blog post.

# Demonstration : (Without Lombok)

Let's create a Person class. Full code here


//demo1
package com.stevenmwesigwa.gettersetter.demo1;
		
public class Person {

		private String firstname;
		private String lastname;
		
		public String getFirstName() {
				return firstname;
		}
		public String getLastName() {
				return lastname;
		}
		public void setFirstName(String firstname) {
			 this.firstname = firstname;
		}
		public void setLastName(String lastname) {
				this.lastname = lastname;
		}		
}

With lombok, most of this boilerplate code could easily be eliminated by adding a few annotations here and there.

# Demonstration : (With Lombok)

Let's create a Person class but this time using lombok specific annotations.. Full code here


//demo2
package com.stevenmwesigwa.gettersetter.demo2;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Person {

		private String firstname;
		private String lastname;
}

NOTE: Adding the annotations outside the class definition will implicitly generate getter and setter methods for all fields. It's possible to set @Getter and @Setter annotations individually for each field.

By adding @Getter and @Setter annotations, the respective getter and setter methods will be added for us automatically.

We can still make use of the getter and setter methods as if we had created them manually.

# Using generated getter and setter methods by project lombok

We'll create a class called App.java which will make use of our Person.java class. Full code here


//demo2
package com.stevenmwesigwa.gettersetter.demo2;

public class App {

    public static void main(String[] args) {
        Person person = new Person();
        person.setFirstName("Steven");
        person.setLastName("Mwesigwa");
    
    }
}

As you can notice above, We're using setter methods as though we had created them manually.

NOTE: Adding a plain @Getter or @Setter annotation will respectively by default generate methods with public level accessibilty.

To change the accessibilty level, You would have to explicity set it.

# Explicitly setting the AccessLevel for generated getter and setter methods with lombok

Let's create a Person class but this time changing setLastname() method to have protected level accessibilty. Full code here


//demo3
package com.stevenmwesigwa.gettersetter.demo3;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class Person {
    //...
    @Getter @Setter private String firstname;
    @Setter(AccessLevel.PROTECTED) private String lastname;

}

TIP: You have other options for setting the access level for your generated methods. i.e PRIVATE, PROTECTED, PACKAGE and PUBLIC .

That's it for today. Don't hesitate to leave a comment. Your feedback is greatly appreciated.

About The Author   

Steven Mwesigwa

Software Engineer at Vogue Book Consultancy Services Ltd


Picture Of Steven Mwesigwa

Steve is currently a software developer at Vogue Book Consultancy Services Ltd and a technology author. He holds a Dip. in civil engineering from Kyambogo University. He founded...