1

I want to make use of the new java.util.Optional in my beans.

According to this question the attributes should be passed to the constructor as simple types, like this:

class Foo {
    Optional<A> optionalA;

    public Foo(A a){
        optionalA = Optional.ofNullable(a);
    }
}

but, what about the setter method for the attribute?

should it be:

public void setOptinalA(Optional<A> newOptionalA){
    optionalA = newOptionalA;
}

or:

public void setOptinalA(A newA){
        optionalA = Optional.ofNullable(newA);
    }

are the two of them compliant with the JavaBean specification?

Thanks in advance

Community
  • 1
  • 1

0 Answers0