1

In Java 9, I can write code like this:

enum Abc {
    A, B, C;
    static protected int foo = 4; // what is the purpose of the protected variables like this in enum?
}

I think it makes no sense, because we cannot inherit or implement an enum.

Edit: This question is the same with Why are protected members allowed in final java classes?

1 JustOnly 1
  • 171
  • 11

1 Answers1

3

It means just what it always means: accessible only from subclasses or classes in the same package. You're right that since you can't inherit the class, it's not any different from a package-private field in practice.

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • Using `protected` modifier for a variable can be right option ? Perhaps no one uses this modifier in `enum` for a variable. right ? – 1 JustOnly 1 Mar 18 '18 at 01:08
  • 1
    If it does what you want, it's a right option. :-) I agree that stylistically, package-private is probably better. – yshavit Mar 18 '18 at 01:12