title: Scope of variables inside switch case statement in Java
body: <p>I have the following code, my question is how can we persist the value of String A and B and use it in another case statement for comparing it.</p>
  <pre><code>String A = null;
  String B = null;
  Switch(key) {
  
     case “ABC”:
       A = “Hello”;
     break;
     case “XYZ”:
       B = “Hi”;
      break;
     case “compare”:
        A.equals(B); //Exception here as A and B are nulls.
  </code></pre>
  <p>Can anyone please guide me here as I’am really confused with the variable scope in switch case.</p>