Mutable boolean field in Java

前端 未结 9 2230
挽巷
挽巷 2020-12-09 07:32

I need a mutable boolean field in Java (I will return this field via get* method later and it should be possible to modify this field).

Boolean doesn\'t work because

9条回答
  •  独厮守ぢ
    2020-12-09 08:18

    Immutable classes are easier to work with. They'll never change and there will be no problems with concurrent code. (Basically, there are fewer possibilities to break them.)

    If you would like to return a reference to your Boolean value, you can use java.util.concurrent.atomic.AtomicBoolean if you're working with multiple threads or plain old org.apache.commons.lang.mutable.MutableBoolean.

提交回复
热议问题