Mutable boolean field in Java

前端 未结 9 2260
挽巷
挽巷 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:12

    Are you really saying that you want callers to be able to modify the object's boolean value by manipulating what gets returned? So that the object and caller would share a reference to it?

    Just so I understand, given:

    class OddClass {
       private Boolean strangeFlag;
       public Object getAttrbiute(String attributeName) { 
          if (attributeName.equals("strangeflag")) return (Object)strangeFlag; 
          ...
       }
    }
    

    And then caller does:

       Boolean manipulableFlag = (Boolean) myOddClass.getAttrbiute ("strangeflag");
    

    And then later, if caller changes the value of manipulableFlag, you want that change to happen in the OddClass instance, just as though caller had instead used a setAttrbiute method.

    Is that what you're asking?

    In that case, you'd need a holder class, as suggested by Adam.

提交回复
热议问题