Why can’t I create an enum in an inner class in Java?
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.
4 Answers 4
This is allowed in Java 16+
This is no longer forbidden starting with Java 16 (and the later LTS release Java 17) thanks to JEP 395 relaxing the rules on some static types inside inner classes.
All of the rules that apply to nested classes apply to inner classes. In particular, an inner class may declare and inherit static members (§8.2), and declare static initializers (§8.7), even though the inner class itself is not static .
Before Java 16 it was forbidden:
enum types that are defined as nested types are always implicitly static (see JLS §8.9. Enums):
A nested enum type is implicitly static .
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):
It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable
Therefore you can’t have an enum inner type inside a non-static nested type.
Enum Inside Class
I am trying to define an enum within a class but Eclipse continues to underline my code in the lovely red color. Here is what my code looks like for the most part:
package campusvilleFoodBank; import java.util.NoSuchElementException; import java.util.Scanner; public class FoodArrayList < public enum Days private Food [] foodArray; private int iterator; private boolean startIndexing; private static final int MAX = 24; public FoodArrayList() < foodArray = new Food [MAX]; iterator = 0; >public FoodArrayList(int arraySize) < foodArray = new Food [arraySize]; iterator = 0; >//Rest of code, with more complicated methods here >
I must be missing something very simple, it’s likely to be a syntax error. The code that follows involves methods such as my toString() , some sorting methods for the foodArray instance variable, and things along those lines. I do not have a main method defined. I know that I can define an enum within a class definition and all of the code I’ve seen elsewhere is written nearly exactly the same as to what I have. However, Eclipse does not seem to recognize even the enum declaration and I don’t believe I need to import anything. Here is what I see in Eclipse for the code I have an error with: For comparison, here is some enum code from another, separate .java file that appears to be without error: Any assistance on this would be appreciated. Let me know if further clarification is needed.
Java public enum in class
* Информация для продвинутых юзеров. На самом деле возможно ограниченное наследование от Enum. Подробнее здесь: https://kibungo.livejournal.com/19999.html Пример наследования и переопределения общего метода:
public enum MyEnum < VAL1 < public int returnMyVal()< return 20; >>, VAL2; public int returnMyVal() < return 10; >> ------------------------ System.out.println(MyEnum.VAL1.returnMyVal()) // 20 System.out.println(MyEnum.VAL2.returnMyVal()) // 10
«По сравнению с обычными классами, на Enum наложили одно серьезное ограничение — от него невозможно наследоваться.» Что за бред вы пишете?! Все перечисления изначально неявно расширяют класс java.lang.Enum, а поскольку в Java нету множественного наследования, то и расширять enum уже больше ничего не может.
В статье enum в коде с маленькой буквы, в комментариях к коду — Enum с большой Почему не говорится, что это разные вещи? Если у нас несколько enum`ов с перечислениями в приложении и нам нужно передать один из них как аргумент в метод можно сделать просто так someMethod(Enum e) < и здесь написать условие для выбора одного из enum`ов >Т.е. не нужно приводить все enum`ы к единому интерфейсу или абстрактному классу, чтобы выбрать один из enum`ов Как я понял Enum(тот который с большой буквы) — это что-то типа пустого интерфейса для всех enum`ов
@Override public String toString() < return "DayOfWeek';
В коде приведенного примера про школьника есть ошибка: задано поле private ScholarSchedule schedule, но сеттер для него не прописан, а так как оно изначально не инициализировано, получаете NullPointerException. Добавьте сеттер (либо просто сделайте это поле public, что есть костыль) для задания значения:
public void setSchedule(ScholarSchedule schedule)
Ну или, в конце концов, поместите метод main в класс Scholar (видимо так и задумывалось автором примера изначально), тогда он будет иметь доступ к private ScholarSchedule schedule и сможет инициализировать его надлежащим образом.
Using Java enums from different classes?
I would effectively have an enum in one class that is used in another class (in my case, specifically in a switch-case statement). However, when I do that, I get an error like:
All the answers seemed to be correct, but when I did that it started giving the error an enum switch case label must be the unqualified name of an enumeration constant and I found that simply removing everything in front of the status ( Status.Opened became Opened ) it fixed it.
7 Answers 7
An enum switch case label must be the unqualified name of an enum constant:
switch (test.getStatus()) // returns the current status < case Opened: // do something // break and other cases >
It doesn't matter that it's defined within another class. In any case, the compiler is able to infer the type of the enum based on your switch statement, and doesn't need the constant names to be qualified. For whatever reason, using qualified names is invalid syntax.
This requirement is specified by JLS §14.11:
SwitchLabel: case ConstantExpression : case EnumConstantName : default : EnumConstantName: Identifier
(Thanks to Mark Peters' related post for the reference.)
@AmirAfghani - I almost went the same route, if you look at my edit history 🙂 Luckily I tested it and remembered about enums.
If your getStatus() returns in fact a Status your case should be :
your IDE will give you an error like :
an enum switch case label must be the unqualified name of an enumeration constant
As the Status enum is enclosed in the Test class you need to use Test.Status instead of just Status .
It needs to be entirely unqualified, the qualification is given by the type of the switch() ed variable (in this case test.getStatus() , which is a Test.Status ).
Your Enum Status is a part of your class Test . As such, you need to qualify it:
Test test = new Test(); // new Test object (storing enum) switch(test.getStatus()) // returns the current status < case Test.Status.Opened: // do something // break and other cases >
Enums are (more or less) just classes like any other, so the rules here are the same as for other inner classes. Here, you probably meant to declare the enum class as static : it doesn't depend on members of its enclosing class. In that case, Test.Status.Opened would be a correct way to refer to it. If you really don't mean the class to be static, you probably need to use an instance as the "namespace" qualifier, ie test.Status.Opened .
Edit: Apparently enums are implicitly static. This makes some sense given what an enum is supposed to be, but it's probably good form to declare them as static explicitly.
I had declared an enum in my class as follows :
public class MyBinaryTree
private Node root; public enum ORDER_TYPE < IN_ORDER, POST_ORDER, >; public void printTree(ORDER_TYPE order) < //Printing tree here >
and I was trying to access this in a different class
/** * @param args */ public static void main(String[] args) < // TODO Auto-generated method stub MyBinaryTree myTree = new MyBinaryTree(); myTree.printTree(myTree.ORDER_TYPE.POST_ORDER); >
The problem here is if I use an instance object to access the enum ORDER_TYPE I get a compile time error : "ORDER_TYPE cannot be resolved or is not a field"
I checked again and changed my code to directly use the class name as the name qualifier
public static void main(String[] args)
This solved the issue - I believe that whenever we are using the enum of one class in another - we should access it via the class like a static method.
Using enum as a class in java
And now if I use changeState(State.NOT_ACTIVE) it will change state in SomeObjects and in all Objects in list or only in SomeObjects? If only in SomeObjects, what should I do to change state in all Objects in list with changeState() ? Is the creating of class, containing that enum, the only way in this case? UPDATE: thanks, but in my case i need to change state of all objects with
5 Answers 5
And now if I use changeState(State.NOT_ACTIVE) it will change state in SomeObjects and in all Objects in list or only in SomeObjects?
I'm not entirely sure what your line of thought is here.
If you call changeState(NOT_ACTIVE) on some instance of SomeObjects , then as per the definition of the method it will set that object's state field to be NOT_ACTIVE . There is no way that this single method call would change multiple objects, as it's performing a single assignment on a single variable.
If only in SomeObjects, what should I do to change state in all Objects in list with changeState()?
If you want the changeState method to apply the state to all of the objects contained within the objects field, then you can make it do this with a simple loop:
public void changeState(State state) < this.state = state; for (SomeObject obj : objects) < obj.state = state; >>
Is the creating of class, containing that enum, the only way in this case?
Again it isn't clear to me exactly what you're thinking of here. Don't you want to set the state value of all the SomeObject instances contained within a SomeObjects instance? That is, set values on instances that already exist, of a class that's already defined?
If so then this can be done as above by setting the values in a loop. The fact that the field is an enum makes no difference; any primitive or reference type would behave identically here.
Edit to answer comment: Ah, it sounds like the root problem was using a field in the first place.
Given that you want all of the contents of the objects collection to have exactly the same state at all times, it is probably wrong for them to all have an individual state field. It's a modelling error, in that a SomeObject instance doesn't really independently hold/control its own state.
Something I was going to suggest earlier is that you should have implemented methods rather than accessing the fields directly - and that's something that comes to the fore now. Rather than SomeObject having a state field, it should have a getState() method. With a given instance you have a way to get its current state - but this isn't necessarily implemented as a direct object reference.
And the implementation of this method should simply be to get the state of the wrapping SomeObjects instance (that's not a particularly informative class name). So it might be implemented as following:
public class SomeObject < private final SomeObjects parent; public SomeObject(SomeObjects parent) < this.parent = parent; >public State getState() < return parent.state; // Ideally accessed via method too >>
This is consistent with how I understand your design - each SomeObject has the same state as its wrapping SomeObjects (and so implicitly must have exactly one parent SomeObjects instance defined at all times). The code above is an almost exact representation of that design.
(And there's still nothing special here with enums; things would work identically if you were returning int or String or Map> .)