15

I have a very specific issue that has to do with an inner class. Let me show you some sample code:

class Foo {
    MYOPTIONS temp;

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

So this enumeration is inside the class Foo. Now what I want to do is set the temp variable as one of the three options, but do that outside the class Foo, let's say from a class called External. Unfortunately I cannot have a set method to do that because External.setTemp (MYOPTIONS.OPTION1) is not valid, as the enum is not visible in class external. So the only thing I could come up with is have three methods in class Foo:

public void setTempOption1 () {this.temp=MYOPTIONS.OPTION1;}
public void setTempOption2 () {this.temp=MYOPTIONS.OPTION2;}
public void setTempOption3 () {this.temp=MYOPTIONS.OPTION3;}

Obviously the other option is to change the enumeration and not have it as an inner class. Are there any other options that I am missing? Thanks

user unknown
  • 35,537
  • 11
  • 75
  • 121
sakis kaliakoudas
  • 2,039
  • 4
  • 31
  • 51
  • 2
    you don't need to change the enumeration, you just need to make the inner class (Foo) not-inner – Janus Troelsen Apr 28 '12 at 10:11
  • Scoping does not work in the opposite direction. If you declare a variable in a method, you don't expect it to be defined outside of the method. It's the same concept, just in a more abstract way. – Corbin Apr 28 '12 at 10:14
  • if you're using eclipse, there are refactoring tools that can automatically move the enum or the class to it's own file and make them public or package-private – Janus Troelsen Apr 28 '12 at 10:14
  • Foo is not an inner class, MYOPTIONS is and I have no clue why I wrote it with caps :P Seems like I don't have another choice other than the 2 I suggested myself. – sakis kaliakoudas Apr 28 '12 at 10:17
  • OPTION 2 has a blank too much, needs a comma, and OPTION3 a semicolon. Showing the test code and using correct indentation would be nice. – user unknown Apr 28 '12 at 10:20
  • See http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html - The Static Nested Classes part – ccheneson Apr 28 '12 at 10:22
  • the semicolon is optional in the end! still 2 typos in there :( – sakis kaliakoudas Apr 28 '12 at 10:25
  • Note: All enums are static in Java, no need to declare that. – EntangledLoops Apr 12 '19 at 16:54

5 Answers5

28
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.

user unknown
  • 35,537
  • 11
  • 75
  • 121
  • just noticed that you were the first one to answer, thanks : ) – sakis kaliakoudas Apr 28 '12 at 10:30
  • 2
    IntelliJ says: modifier static is redundant for inner enums. See: http://stackoverflow.com/questions/253226/nested-java-enum-definition-does-declaring-as-static-make-a-difference – Kuchi Sep 03 '15 at 16:43
2

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

Ignux02
  • 111
  • 3
1

You can do it like this:

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

Although I also would recommend to externalise MYOPTIONS.

Anthales
  • 1,158
  • 6
  • 10
1

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;
    }
}
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
0

Internally every enum constant return the object of type enum;(i.e Foo.MYOPTIONS.OPTION1 returns object of type MYOPTIONS) so we cant make our own intantiation(i.e new Foo.MYOPTIONS(); not allowed to create enum object explicitly becoz of num itself provide the object)

so, if we want to print reference variable by default .toString(); method will be invoked. In enum toString(); mehod overidden and its return the name of the constant. so we can have both Object and enum constant by one line (i.e Foo.MYOPTIONS.OPTION1) example:

    class Foo {
        MYOPTIONS temp;

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

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

that's it,if you write this line to instantiate enum object explicitly

(foo.temp = new Foo.MYOPTIONS();)

enter code here

you can get compile time error like:--

error: enum types may not be instantiated