1

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?

5
  • A cat is a cat, a dog is a dog, it's the same for E1 and E2 Commented Oct 17, 2016 at 20:28
  • That's not possible with enums in Java. You can have the enums implement interfaces, and that's it. Commented Oct 17, 2016 at 20:29
  • 2
    You're not using the right tool for the job if you're trying to do this ... Commented Oct 17, 2016 at 20:38
  • 1
    What are you really trying to do? If you tell us what E1, E2 and A and B really are, you might get better answers. Commented Oct 17, 2016 at 21:02
  • did you find any of the answer above helpfull? Commented Oct 19, 2016 at 3:40

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I assume even if the two enums have the exact same staff inside them (say both E1 and E2 have 'A' and 'B'), this is still not possible, right?
@Babak Correct. A name is just a name. The compiler and the application itself knows the difference, though. Instrument.BASS and Fish.BASS are two separate entities.
2

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;

Comments

0

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.

Comments

0

Well - looking at your second snippet, what you could do is to define E2 as

enum E2 {
    A, B
}

and E1as

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.

Comments

0

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)

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.