0

java.util.Optional class helps prevent null pointer exceptions when used as a method return type instead of returning 'raw' objects (those may be null, potentially resulting in NPE).

However, what about other uses? Is it a good idea to use Optional as object field(s) type? Static class fields? I.e.,

class MyClass {
    private Optional<String> myValue; // does it look reasonable?
}

Thanks!

Sergei
  • 1
  • 1
  • 2
  • Why not just wrap it every time you use `myValue`? –  Dec 27 '17 at 23:28
  • You can look for your answer https://stackoverflow.com/questions/29033518/is-it-a-good-practice-to-use-optional-as-an-attribute-in-a-class – Harleen Jun 12 '18 at 07:52

0 Answers0