-1

Some time i look some people make final field and create Lombok look like:

@AllArgsConstructor(access = AccessLevel.PRIVATE) 
    public class Person {

        private final String firstName;

        private final String middleName;

        private final String lastName;

Some time field not mark final. When we mark final field in @Entity and when we not ?

soorapadman
  • 4,451
  • 7
  • 35
  • 47
Tbtrungdn
  • 31
  • 4

2 Answers2

0

You make a field final once you do not want the field to be changed after the object has been constructed.

It can also help in multithreading environment to guarantee safe object initialization and sharing.

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
0

In Lombok and in @AllArgsConstructor annotation you can use final keyword to make sure that object will not change after initialization in constructor. This annotation will get all fields to build your constructor.

There is also @RequiredArgsConstructor annotation which is using fields marked with final to build constructor:

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Person {
    private final String firstName;
    private final String middleName;
    private String lastName;
}

An you constructor will look like this after delombok:

@java.beans.ConstructorProperties({"firstName", "middleName"})
private Person(String firstName, String middleName) {
    this.firstName = firstName;
    this.middleName = middleName;
}