Lombok's @Getter @Setter operation memo

Introduction

This is a memo of the result of operation verification focusing on @ Getter and @ Setter of Lombok. I won't write about how to install Lombok and how to use annotations other than @ Getter and @ Setter here, so please see what other people have put together.

About Lombok

Before getting into the main subject, I will briefly explain Lombok. Lombok is a library that automatically generates methods such as getter, setter, and toString by using specific annotations.

You can reduce the amount of description such as POJO objects at the development stage. There may be few people who write getters and setters by themselves these days, but ** spelling mistakes ** can be eliminated because they are automatically generated.

Practice

Let's use it now.

When you want to create getters and setters for specific fields

In this case, add @ Getter and @Setter to the fields for which you want to create getters and setters.

Product.java


import lombok.Getter;
import lombok.Setter;

public class Product {

	@Getter
	@Setter
	private long id;

	@Getter
	private String name = "mouse";
}

If you check the outline, you can see that the getter and setter specified for the field have been created. image.png

Check if it actually works.

public static void main(String[] args) {
	Product product = new Product();
	product.setId(1L);

	System.out.println(product.getId());
	System.out.println(product.getName());
}

console


1
mouse

You can see that each is working correctly like this.

If you want to create getters and setters for all the fields in the class

Earlier we gave the fields @ Getter and @Setter, but in this case we give the class @ Getter and @Setter.

Product.java


import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Product {

	private long id;

	private String name;
}

You can see that getters and setters have been created for all the fields in the class. image.png

Operation verification is omitted.

If you want to create getters for all fields and setters for some fields

In this case, annotate the class if you want to apply it to all fields, and annotate the field if you want to apply it to some fields.

Product.java


import lombok.Getter;
import lombok.Setter;

@Getter
public class Product {

	@Setter
	private long id;

	private String name;
}

image.png

As you specified, the setter is created only in ʻid`.

If you do not want to create getters and setters for only some fields

Specify ʻAccessLevel.NONE if you want to create getters and setters for almost every field in your class, but not for some fields. ʻAccessLevel allows you to change the access modifiers of the getters and setters that are created. If nothing is specified, it will be public.

Product.java


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

@Getter
@Setter
public class Product {

	@Getter(AccessLevel.NONE)
	private long id;

	@Setter(AccessLevel.NONE)
	private String name;
}

image.png

As you can see, the getter and setter with ʻAccessLevel.NONE` specified have not been created.

When you want to change the behavior of getter and setter from the default

During development, you will come across a case where you want to change the behavior of getters and setters only for a part of the class. In that case, implement getters and setters for the fields whose behavior you want to change. This time, let's implement only the getter of the name field by ourselves.

Product.java


import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Product {

	private long id;

	private String name;

	public String getName() {
		return "Product name: " + name;
	}
}

From the outline, the order has changed, but you can see that there is a getter. image.png

Let's move it in the same way as before.

public static void main(String[] args) {
	Product product = new Product();
	product.setId(1L);
	product.setName("keyboard");

	System.out.println(product.getId());
	System.out.println(product.getName());
}

console


1
Product name:keyboard

You can see that the getter implemented by yourself is called like this.

Summary

reference

Recommended Posts

Lombok's @Getter @Setter operation memo
Docker operation memo
About getter setter
Bit operation speedup memo
java setter getter error resolution
Personal memo Lombok's typical annotation
Automatic generation of constructor, getter / setter