@FieldDefaults

Since

@FieldDefaults was introduced as experimental feature in lombok v0.11.4.

Experimental

Experimental because:

  • New feature; unsure if this busts enough boilerplate
  • Would be nice if you could stick this on the package-info.java package to set the default for all classes in that package
  • Part of the work on @Value, which is experimental
Current status: positive - Currently we feel this feature may move out of experimental status with no or minor changes soon.

Overview

The @FieldDefaults annotation can add an access modifier (public, private, or protected) to each field in the annotated class or enum. It can also add final to each field in the annotated class or enum.

To add final to each (instance) field, use @FieldDefaults(makeFinal=true). Any non-final field which must remain nonfinal can be annotated with @NonFinal (also in the lombok.experimental package).

To add an access modifier to each (instance) field, use @FieldDefaults(level=AccessLevel.PRIVATE). Any field that does not already have an access modifier (i.e. any field that looks like package private access) is changed to have the appropriate access modifier. Any package private field which must remain package private can be annotated with @PackagePrivate (also in the lombok.experimental package).

With Lombok

01 import lombok.AccessLevel;
02 import lombok.experimental.FieldDefaults;
03 import lombok.experimental.NonFinal;
04 import lombok.experimental.PackagePrivate;
05 
06 @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE)
07 public class FieldDefaultsExample {
08   public final int a;
09   int b;
10   @NonFinal int c;
11   @PackagePrivate int d;
12   
13   FieldDefaultsExample() {
14     a = 0;
15     b = 0;
16     d = 0;
17   }
18 }

Vanilla Java

01 public class FieldDefaultsExample {
02   public final int a;
03   private final int b;
04   private int c;
05   final int d;
06   
07   FieldDefaultsExample() {
08     a = 0;
09     b = 0;
10     d = 0;
11   }
12 }

Supported configuration keys:

lombok.fieldDefaults.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @FieldDefaults as a warning or error if configured.
lombok.fieldDefaults.defaultPrivate = [true | false] (default: false)
(Since 1.16.8) If set to true, every field in every class or enum anywhere in the sources being compiled will be marked as private unless it has an explicit access modifier or the @PackagePrivate annotation, or an explicit @FieldDefaults annotation is present to override this config key.
lombok.fieldDefaults.defaultFinal = [true | false] (default: false)
(Since 1.16.8) If set to true, every field in every class or enum anywhere in the sources being compiled will be marked as final unless it has the @NonFinal annotation, or an explicit @FieldDefaults annotation is present to override this config key.

Small print

Like other lombok handlers that touch fields, any field whose name starts with a dollar ($) symbol is skipped entirely. Such a field will not be modified at all.