title: Assign Enum Values in Java
body: <p>If I have two enums, say:</p>
  
  <pre><code>public enum E1 {
      A, B
  }
  
  public enum E2 {
      A, B
  }
  </code></pre>
  
  <p>Is there any way I can assign a value of the enum type E2 to the value of type E1, like this:</p>
  
  <pre><code>E2 var = E1.B;
  </code></pre>
  
  <p>(Assuming that I know in advance that B exists in both enums)</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I understand that Java doesn't allow this. Maybe I should explain this as well then. I have a variable (x) of type <strong>enum E1</strong> (it can be A or B). For the task that I'm trying to achieve, I need to have another variable of type <strong>enum E2</strong> with the same value. The way that I'm handling this right now is basically:</p>
  
  <pre><code>if (x == E1.A) {
      E2 var = E2.A;
  } else {
      E2 var = E2.B;
  }
  </code></pre>
  
  <p>So I guess what I'm asking this is that is there any nicer way to do this?</p>