Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marking optional member variable as NonNull

One of the String member variables of my class is Optional. Does it makes sense to mark it with Lombok annotation @NonNull ?

I would like to ask the same question in case that member variable is Map instead of String. Will the answer change ?

like image 753
Andy897 Avatar asked Feb 17 '26 11:02

Andy897


1 Answers

Optional main point is to make fluent API. Field is not and should not be a part of API.

What is the difference for you between Optional<Object> obj and Object obj in fields?

Both of fields could be annotated with @NotNull. Annotating a field with @NotNull will give you the same result as annotating an Optional but without any extra calls like ifPresent(). If you want to use Optional to apply chaining operations to a field - you are using Optional in a wrong way. There in no reason to use Optional in field.

You can read more in openjdk mailing list optional discussion.

Having Optional enables to do fluent API mail.openjdk

like image 92
Sergei Rybalkin Avatar answered Feb 20 '26 02:02

Sergei Rybalkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!