I am trying to understand scope in Java. In Perl, I can do the following:
my $x = 1;
{
my $x = 2;
say $x;
}
say $x;
In other words, since I declared the variable $ x c mywithin the scope, the variable $ x in this scope is local to that scope (that is, not the same variable as the global variable $ x). Now, in Java, I'm trying to do something like this, but I get an error
The rawURL variable is already defined in the run () method
Here is the code:
URI rawURl;
try {
rawURL = new URI("http://www.google.com");
} catch (Exception e) {
}
for (Element link : links) {
URI rawURL;
try {
rawURL = new URI(link.attr("abs:href"));
} catch (Exception e) {
}
}
So the thing is, I don’t want to have to create declarations for all my variable names, ensuring that each one is different. I just use rawURL to create a normalized URL, so it is essentially a temporary variable. I could just do this:
for (Element link : links) {
rawURL = new URL(..);
}
, . , ?