236. Do not use Optional for fields

The do not use category continues with the following statement—Optional was not intended to be used for fields and it doesn't implement Serializable.

The Optional class is definitively not intended to be used as a field of a JavaBean. So, do not do this:

// Avoid
public class Book {

[access_modifier][static][final]
Optional<String> title;
[access_modifier][static][final]
Optional<String> subtitle = Optional.empty();
...
}

But do this:

// Prefer
public class Book {

[access_modifier][static][final] String title;
[access_modifier][static][final] String subtitle = "";
...
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.51.157