title: Variables in different switch cases can't have the same name?
body: <p>I was refactoring some code to make it easier to read and I ran into something that I find weird and I was wondering if anyone could explain this to me.</p>
  
  <p><strong>Original code:</strong></p>
  
  <pre><code>if(tokensLeft == 3) {
    String id = tokens.nextToken();
    String value = tokens.nextToken();
    String trailerId = tokens.nextToken();
    rawListener.binaryInfo(id, Integer.parseInt(value), trailerId, this);
  } else if(tokensLeft == 2) {
    String id = tokens.nextToken();
    String value = tokens.nextToken();
    rawListener.binaryInfo(id, Integer.parseInt(value), this);
  } else {
    System.out.println("Method call binaryInfo could not be done because: \"Wrong number of parameters\"");
  }
  </code></pre>
  
  <p><strong>After refactoring:</strong></p>
  
  <pre><code>switch(tokensLeft) {
  case 3:
    String id = tokens.nextToken();
    String value = tokens.nextToken();
    String trailerId = tokens.nextToken();
    rawListener.binaryInfo(id, Integer.parseInt(value), trailerId, this);
    break;
  case 2:
    String id = tokens.nextToken(); // Syntax error
    String value = tokens.nextToken(); // Syntax error
    rawListener.binaryInfo(id, Integer.parseInt(value), this);
    break;
  default:
    System.out.println("Method call binaryInfo could not be done because: \"Wrong number of parameters\"");
    break;
  }
  </code></pre>
  
  <p>At first glance this looks perfectly reasonable, but this gives me a syntax error.</p>
  
  <blockquote>
    <p>Link all references for a local rename (does not change references in other files)</p>
  </blockquote>
  
  <p>It turns out that for some reason in a switch statement, I am unable to use the <code>String id</code> and <code>String value</code> again in a different case.</p>
  
  <p>This makes naming my variables rather awkward.</p>
  
  <p>Now you could say: "Just declare your variables above your switch statement." But that would mean that I <strong>always</strong> create my variables, even if <code>tokensLeft</code> is neither 3 or 2 and I wouldn't need my variables. That just feels like using unnecessary memory.</p>
  
  <p>Can anyone explain to me why the switch case does this and how I could solve my problem?</p>