Using Java enums from different classes?

2019-03-22 15:08发布

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?

标签: java class enums
7条回答
来,给爷笑一个
2楼-- · 2019-03-22 15:38

If your getStatus() returns in fact a Status your case should be :

case Opened:

If you try:

case Test.Status.Opened:

your IDE will give you an error like :

an enum switch case label must be the unqualified name of an enumeration constant
查看更多
登录 后发表回答