Reputation: 487
If I have two enums, say:
public enum E1 {
A, B
}
public enum E2 {
A, B
}
Is there any way I can assign a value of the enum type E2 to the value of type E1, like this:
E2 var = E1.B;
(Assuming that I know in advance that B exists in both enums)
Edit:
I understand that Java doesn't allow this. Maybe I should explain this as well then. I have a variable (x) of type enum E1 (it can be A or B). For the task that I'm trying to achieve, I need to have another variable of type enum E2 with the same value. The way that I'm handling this right now is basically:
if (x == E1.A) {
E2 var = E2.A;
} else {
E2 var = E2.B;
}
So I guess what I'm asking this is that is there any nicer way to do this?
Upvotes: 1
Views: 3477
Reputation: 101
Since you can define enums
as 'lightweight classes' conforming to interfaces with their own properties and methods, you can write up your combining rules something like this:
Upholstery.java
import java.util.List;
public interface Upholstery {
public List<Material> combinesWith();
}
Material.java
import java.util.List;
public interface Material {
public List<Upholstery> combinesWith();
}
Upholsteries.java
import java.util.Arrays;
import java.util.List;
public enum Upholsteries implements Upholstery {
SATIN {
@Override
public List<Material> combinesWith() {
return Arrays.asList(Materials.PLASTIC);
}
},
LEATHER {
@Override
public List<Material> combinesWith() {
return Arrays.asList(Materials.MAHOGANY);
}
}
}
Materials.java
import java.util.Arrays;
import java.util.List;
public enum Materials implements Material {
MAHOGANY {
@Override
public List<Upholstery> combinesWith() {
return Arrays.asList(Upholsteries.SATIN,
Upholsteries.LEATHER);
}
},
PLASTIC {
@Override
public List<Upholstery> combinesWith() {
return Arrays.asList(Upholsteries.SATIN);
}
}
}
Obviously, you could add additional methods to the interfaces, or modify the signature of combinesWith()
to implement more complex logic - e.g. combinesWith(FurnitureType f, double budget)
.
Upvotes: 0
Reputation: 48258
The short answer is NO, java is not allowing that
I need more info about what exactly you need to do, but you can achieve something similar doing something like this
public static void main(String[] args) {
E2 myE2 = E2.valueOf(E1.A.name());
System.out.println(myE2);
}
enum E1 {
A, B
}
enum E2 {
A, B
}
and note that will work ONLY because the field A is defined in both enums, otherwise is not going to work failing with an java.lang.IllegalArgumentException: No enum constant ...
but this workaround is exactly what you post when you say
(Assuming that I know in advance that B exists in both enums)
Upvotes: 0
Reputation: 15622
You can do this indirectly if you let both enum implement the same interface:
interface CommonEnumInterface{}
public enum E1 implements CommonEnumInterface{
A, B
}
public enum E2 implements CommonEnumInterface{
C, D
}
CommonEnumInterface myE = E1.A;
myE = E2.D;
Upvotes: 2
Reputation: 8220
Well - looking at your second snippet, what you could do is to define E2
as
enum E2 {
A, B
}
and E1
as
enum E1 {
A(E2.A), B(E2.B);
private E2 e2;
E1(E2 e2) {
this.e2 = e2;
}
public E2 getE2() {
return e2;
}
}
like this you can simply write your if
statement as
E2 var = x.getE2();
The idea behind this solution is, that enums like any other class in Java can have properties (private E2 e2
), constructors (E1(E2 es) {...}
) and methods (public E2 getE2()
). Like that you can "add" any other values to your enum instances and use a getter to query them later on.
Upvotes: 0
Reputation: 2540
No.
E1
and E2
are two different enum types. Imagine you were developing a card game application and you had an enum for Suite and an enum for Rank. What you are asking is equivalent to trying to say that a card can have a Suite of Rank.TEN
or a Rank of Suite.HEARTS
. It just doesn't make sense, and isn't how enums work.
Upvotes: 3
Reputation: 2251
Yeah the answer as commented above, is you can't do it.
Enums in Java are type-safe and have their own namespace. It means your enum will have a type for example "Currency" and you can not assign any value other than specified in Enum Constants. E1 is E1 and E2 is E2 namespace.
Upvotes: 0