0

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 private 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?

3
  • 1
    Can you post a complete compilable example that demonstrates the issue? Commented Jun 26, 2011 at 15:35
  • public MyEnum(int number) is not the constructor. the enum name is myEnum. no capital m in the name Commented Jun 26, 2011 at 15:38
  • 1
    Also, an enum cannot have a public constructor... Commented Jun 26, 2011 at 15:39

4 Answers 4

1

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;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

It may not make sense, but it is allowed.
0

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"
    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.