开发者

java - an enum question

I have encountered a weird problem in my app (java).

I have an enum. Something like that

public enum myEnum implement myIntrface{
   valueA(1),valueb(2),valuec(3),valued(4)
   private int i;
   // and then - a constructor 
   public MyEnum(int number){
        i = number;
   }       


   private MyObj obj = new MyObj;
   // getter and setter for obj
} 

and in another class I have this

   MyEnum.valueA.setObj(new Obj(...))

in briefe - I have an enum with a p开发者_开发知识库rivate instance member that has a set and a get.

So far so good -

The only thing that amazes me is that later on I look at the value of the MyEnum.valueA().obj is null.

there is nothing that updates the value to null, I have even gave it a default value in the constructor and I still see it null later.

any suggestions?


Enums should be un-modifiable classes so you shouldn't really be doing this. If your looking to modify the state of a type based object like an enum you should use an final class approach with embedded constants. Below is an example of a class based approach with a modifiable name an a un-modifiable name...

public final class Connection {

    public static final Connection EMAIL = new Connection("email");
    public static final Connection PHONE = new Connection("phone");
    public static final Connection FAX = new Connection("fax");
    /**/
    private final String unmodifiableName; //<-- it's final
    private String modifiableName;

    /*
     * The constructor is private so no new connections can be created outside.
     */
    private Connection(String name) { 
        this.unmodifiableName = name;
    }

    public String getUnmodifiableName() {
        return unmodifiableName;
    }

    public String getModifiableName() {
        return modifiableName;
    }

    public void setModifiableName(String modifiableName) {
        this.modifiableName = modifiableName;
    }

}


The purpose of enums is to represent constant values. It does not make any sense to set the fields of a constant value.

You should declare your fields as final, and use the constructor to initialize all of them.


For reference, the following code works as expected:

public class Test {

    public static enum MyEnum {
        valueA(1),valueb(2),valuec(3),valued(4);
        private int i;
        private Object o;

        private MyEnum(int number) {
             i = number;
        }

        public void set(Object o) {
            this.o = o;
        }

        public Object get() {
            return o;
        }


     } 

    public static void main(String[] args) {
        System.out.println(MyEnum.valueA.get());  // prints "null"
        MyEnum.valueA.set(new Integer(42));
        System.out.println(MyEnum.valueA.get());  // prints "42"
    }
}


the cause of this problem is the db40 framework . It loads an enum from the db using reflection. This is well documented .
http://developer.db4o.com/Forums/tabid/98/aft/5439/Default.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜