3

I have the following class along with a nested enum:

public class MyClass {

    private Integer field;

    private enum SelectedValue{
        ALL {
            @Override
            public Integer getAmount() {
                return field; //Error: field cannot be resolved to a variable
            }
        };


        public abstract Integer getAmount();
    }
}

Is it possible to get enclosing instance within the enum body?

user3663882
  • 6,957
  • 10
  • 51
  • 92
  • 3
    possible duplicate of [In Java, are enum types inside a class static?](http://stackoverflow.com/questions/663834/in-java-are-enum-types-inside-a-class-static) – Brett Okken Mar 04 '15 at 13:11

2 Answers2

7

Nested enum types are implicitly static, see JLS 8.9. You cannot access a non-static field of enclosing class

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
3

You can only access field if it is static.

henry
  • 5,923
  • 29
  • 46