title: Local variable in Java with the same name as a global variable
body: <p>I'm trying to understand scope in Java. In Perl, I can do the following:</p>
  
  <pre><code>my $x = 1;
  {
      my $x = 2;
      say $x; # prints 2
  }
  say $x; # prints 1
  </code></pre>
  
  <p>In other words, since I declared the variable $x with <code>my</code> within the scope, the $x variable in that scope is local to that scope (i.e., not the same variable as the global $x variable). Now, in Java I am trying to do something similar, but I am getting the error </p>
  
  <blockquote>
    <p>Variable rawURL is already defined in method run()</p>
  </blockquote>
  
  <p>Here's the code:</p>
  
  <pre><code>// Global rawURL
  URI rawURl;
  try {
    rawURL = new URI("http://www.google.com");
  } catch (Exception e) {
    // Handle later
  }
  
  // Some time later
  for (Element link : links) {
    // rawURL in this scope
    URI rawURL;
    try {
      rawURL = new URI(link.attr("abs:href"));
    } catch (Exception e) {
      // Handle later
    }
  }
  </code></pre>
  
  <p>Now, the thing is, I don't want to have to get creative for all of my variable names, ensuring each one is different. I am simply using rawURL to build a normalized URL, so it's essentially a temporary variable. I could just do this:</p>
  
  <pre><code>for (Element link : links) {
    rawURL = new URL(..);
  }
  </code></pre>
  
  <p>But then that will change the contents of my global variable, and I really don't want that. Is there any way to use the same name for the local variable as I am using for the global variable?</p>