using inner classes in Java - enum

You can do it like this:

Foo f = ...;
f.temp = Foo.MYOPTIONS.OPTION1;

Although I also would recommend to externalise MYOPTIONS.


The declaration is valid, but you have to use it in this way:

Foo.MYOPTIONS var = Foo.MYOPTIONS.OPTION1

You are missing the name of the class when you are using the "enum".


class ContainsInnerEnum {
    MYOPTIONS temp;

    public enum MYOPTIONS {
        OPTION1, OPTION2, OPTION3;
    } 
}

class EnumTester {
    public void test () {
        ContainsInnerEnum ie = new ContainsInnerEnum ();
        // fail:
        // ie.temp = MYOPTIONS.OPTION1;
        // works:
        ie.temp = ContainsInnerEnum.MYOPTIONS.OPTION1;
    }       
}

The whole name of the MYOPTIONS contains the embedding class name.


You have to refer to Foo when using the MYPOTIONS enum:

public class Uta {
    public static void main(String[] args) {
        Foo foo = new Foo();
        foo.temp = Foo.MYOPTIONS.OPTION1;
    }
}

Assuming that the class Foo is in package foo (you should always organize classes in packages), then you can also use static imports, to make your code a bit cleaner:

package foo;

import static foo.Foo.MYOPTIONS.OPTION1;

public class Uta {
    public static void main(String[] args) {
        Foo foo = new Foo();
        foo.temp = OPTION1;
    }
}