Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
SomeEnum.values() is defined in an inaccessible class or interface when doing switch over enum

Let's consider the base class:

package base;

public abstract class Base {
    protected enum BaseEnum {
        FIRST, SECOND, THIRD
    }
}

And its subclass:

package a;

import base.Base;

public class A {
    // The error message appears in case when NestedA is a nested or inner class
    protected /* static */ class InnerA extends Base {
        protected BaseEnum getEnumA() {
            return BaseEnum.FIRST;
        }

        protected int xxx() {
            BaseEnum enumA = getEnumA();
            switch (enumA) {
            // ^^ base.NestedBase.BaseEnum.values() is defined in an inaccessible class or interface
                case FIRST:
                case SECOND:
                    return 1;
                default:
                    return 2;
            }
        }

        protected int xxx2() {
            BaseEnum enumA = getEnumA();
            if (enumA == BaseEnum.FIRST || enumA == BaseEnum.SECOND) {
                return 1;
            } else {
                return 2;
            }
        }

        protected int xxx3() {
            System.out.println(BaseEnum.values());
            return 2;
        }
    }
}

When I am trying to compile it, a compilation error appears on line with switch statement in xxx() saying:

java: base.Base.BaseEnum.values() is defined in an inaccessible class or interface

Briefly the question is what exactly going on here? Why values() is even called here? If it is really called, why is it inaccessible? As you can see, I can properly access BaseEnum and its members from this class (see xxx2()) and even the values() method (which is public method of protected nested enum inside InnerA superclass, see xxx3()). Is access calculated as if this switch would be inside A class? If so, why?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This has been raised as a bug bugs.openjdk.java.net/browse/JDK-8178701 but has not had feedback from the Java maintainers, as far as I can see. So whether it's considered a valid bug is unclear.

over 4 years ago · Santiago Trujillo Report

0

Possible Solvings:

The first solution I found, was that A has to extend from Base, not InnerA. So yout can acces the protected BaseEnum in yout inner class InnerA.

The second solution is simply to make the BaseEnum public.

Why is this so?

The switch statement is the problem, as you presumed in your question. But I have neither a clue nor found something why this is a problem for the switch statement to switch through an protected enum in this constellation.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!