Search code examples
javaenumsparentprotected

Parent's Enum in Java


In the code example below, I'm trying to test the value of an enum in the parent class. The error I get is "p.theEnum cannot be resolved or is not a field.", but it's the exact same code I use in the parent class to test the value (without the p. obviously).

Where am I going wrong? :)

public class theParent {
    protected static enum theEnum { VAL1, VAL2, VAL3 };
    private theEnum enumValue = theEnum.VAL1;

    theParent() { this.theChild = new theChild(this); this.theChild.start(); }

    class theChild {
        private parentReference p;

        public theChild (theParent parent) { this.p = parent; }

        public void run() {
            // How do I access theEnum here?
            if (p.enumValue == p.theEnum.VAL1) { }
        }
    }
}

Solution

  • Just change it to:

    if (p.enumValue == theEnum.VAL1) { }
    

    There's no need to qualify it.

    (Just as an FYI, it would help if you'd make samples like this compile apart from the problem area - I had to make quite a few changes aside from the one above before I could make it compile.)