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

8
  • 2
    you don't need to change the enumeration, you just need to make the inner class (Foo) not-inner Commented Apr 28, 2012 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. Commented Apr 28, 2012 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 Commented Apr 28, 2012 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. Commented Apr 28, 2012 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. Commented Apr 28, 2012 at 10:20

5 Answers 5

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

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

2 Comments

just noticed that you were the first one to answer, thanks : )
IntelliJ says: modifier static is redundant for inner enums. See: stackoverflow.com/questions/253226/…
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".

Comments

1

You can do it like this:

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

Although I also would recommend to externalise MYOPTIONS.

Comments

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

Comments

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

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.