title: Set value of variable in the if block. Access value outside if block
body: <pre><code>if (!pos[1].equals("")) {
      String name = pos[1];
      System.out.println(name);
      int p = 0;
      for (int i = name.length() - 1; i&gt;0; i--){
          if (name.charAt(i)==' ') {
              p = i;  
              break;
          }
          System.out.println(p);
          rank[1] = Integer.parseInt(name.substring(p).trim());
      }
  }
  </code></pre>
  
  <p>In the code, <strong>int p</strong> is set as <strong>i</strong> in the if block. However, when it is accessed outside the if block the value still remains <strong>0</strong> and not the value <strong>i</strong> set in the if condition.</p>