简体   繁体   中英

Convert enum within one class to another in Java

I have the following code in Java, trying to convert enum within one Class to another one:

class Test1 {

    UserType type;

    public enum UserType {
    PENDING,
    ACTIVE,
    INACTIVE,
    DELETED;
    }
}

class Test2 {

    UserType type;

    public enum UserType {
    PENDING,
    ACTIVE,
    INACTIVE,
    DELETED;
    }
}


public class TestClass {
    public static void main(String args[]) {
        Test1 test = new Test1();
        test.type = (Test2.UserType)Test1.UserType.PENDING;
    }
}

Then got the following info: /TestClass.java:29: error: incompatible types: Test1.UserType cannot be converted to Test2.UserType test.type = (Test2.UserType)Test1.UserType.PENDING;

How to cast the enum from one class to another class?

If you do own both classes (Test1 and Test2), I would recommend to refactor the UserType enum into an own file and then reuse the same enum all over the place.

If you are not the owner of those two classes, you have to write a converter, which converts one UserType to the other one, since those are different classes. For the conversion you could eg. use something like the ordinal of the enum value.

Assuming you cannot refactor the code so both classes uses the same top-level enum, you can rely on the fact that the enum values all have the same name:

Test1 test1 = new Test1();
test1.type = Test1.UserType.PENDING;
Test2 test2 = new Test2();
test2.type = Test2.UserType.valueOf(test1.type.name());
System.out.println(test2.type); // prints: PENDING

Of course, you might need to check for null first:

test2.type = (test1.type == null ? null : Test2.UserType.valueOf(test1.type.name()));

There is no relation between the two enums except that they both are enums.

The error message very clearly mentions that both the enums are different entities and hence you can not cast one into other . Both have different namespace and both will be compiled as separate enums, both will exist during the execution of the application as separate enties, both will be loaded as separate.

You have below choices

  1. Extract the enum in separate outer public file and use it when you need it. This will work if you can refactor the code.

  2. Define a class with two methods

     public Test1.UserType getTest1Usertype( Test2.UserType) public Test2.UserType getTest2Usertype( Test1.UserType) 

In both of these methods you can implement the logic for returning the corresponding enums. In your case it seams name as same for instances of both enums so it becomes the comman factor based on which you can do the mappimg.

Note that option 1 is better as in option2 in case there is a change in enum names, or addition of new instances then you need to update these mapping methods. You can use .name() to retrieve the name of type String (which will be the common matching factor between two enums) and to get the enum instance you can use .valueOf(String name)

You cannot assign the one to the other because these are not compatible types.
Objects are compatible for assignment according to their hierarchy and these eunm classes don't belong to the same hierarchy.
Besides, generally duplicating things is a bad smell : you don't want to define twice the enum.

Not an advised solution but to answer to your question : you could make the two classes interoperable by introducing an interface that each enum class implements :

public interface IUserType {
    // define what it is required    
}

class Test1 {

    IUserType type;

    public enum UserType implements IUserType {
        PENDING, ACTIVE, INACTIVE, DELETED;
    }

    public static void main(String args[]) {
        Test1 test = new Test1();
        test.type = Test2.UserType.PENDING;    
    }

}

class Test2 {

    IUserType type;

    public enum UserType implements IUserType {
        PENDING, ACTIVE, INACTIVE, DELETED;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM