If I had a class in Java like this:
public class Test
{
// ...
public enum Status {
Opened,
Closed,
Waiting
}
// ...
}
And I had a different class in a different class file (but in the same project/folder):
public class UsingEnums
{
public static void Main(String[] args)
{
Test test = new Test(); // new Test object (storing enum)
switch(test.getStatus()) // returns the current status
{
case Status.Opened:
// do something
// break and other cases
}
}
}
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:
cannot find symbol - class Status
How would I fix that?
As the
Status
enum is enclosed in theTest
class you need to useTest.Status
instead of justStatus
.I had declared an enum in my class as follows :
public class MyBinaryTree {
}
and I was trying to access this in a different class
public class Solution1 {
}
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
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.
An enum switch case label must be the unqualified name of an enum constant:
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:
(Thanks to Mark Peters' related post for the reference.)
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, ietest.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.
Try with this example
NVM
It needs to be entirely unqualified, the qualification is given by the type of the
switch()
ed variable (in this casetest.getStatus()
, which is aTest.Status
).YourEnum Status
is a part of yourclass Test
. As such, you need to qualify it: