Why can't I create an enum in an inner class in Java?

Steven Roose picture Steven Roose · Feb 13, 2013 · Viewed 40.2k times · Source

What I try to do is this:

public class History {
    public class State {
        public enum StateType {

Eclipse gives me this compile error on StateType: The member enum StateType must be defined inside a static member type.

The error disappears when I make the State class static. I could make State static, but I don't understand why I cannot declare an enum in an inner class.

Answer

Joachim Sauer picture Joachim Sauer · Feb 13, 2013

enum types that are defined as nested types are always implicitly static (see JLS §8.9. Enums)

You can't have a static nested type inside a non-static one (a.k.a an "inner class", see JLS §8.1.3. Inner Classes and Enclosing Instances).

Therefore you can't have an enum inner type inside a non-static nested type.