0

I'm curious if it's possible to assign a custom value during a constructor call using @RequiredArgsConstructor. I.e.

private String value1;
private String value2;
private String value3;

public MyConstructor(String value1, String value2) {
    this.value1 = value1;
    this.value2 = value2;
    this.value3 = createString();
}

Instead I'll be looking for:

@RequiredArgsConstructor
public class MyConstructor {
    @NonNull
    private String value1;
    @NonNull
    private String value2;
    private String value3;

    private String createString() {
        return "test";
    }
}
user3509528
  • 97
  • 10
  • 1
    This might help -> [Is there any "PostConstruct" feature of lombok?](https://stackoverflow.com/a/67815143/7804477) – Gautham M Feb 23 '22 at 05:00

1 Answers1

1

You can assign createString() directly in the definition of the attribute. Therefore, when a new instance is created, also the value3 is initialized.

@RequiredArgsConstructor
public class MyConstructor {
    @NonNull
    private String value1;
    @NonNull
    private String value2;
    private String value3 = createString();

    private String createString() {
        return "test";
    }
}
frascu
  • 747
  • 5
  • 9