Reputation: 844
I'm trying to understand scope in Java. In Perl, I can do the following:
my $x = 1;
{
my $x = 2;
say $x; # prints 2
}
say $x; # prints 1
In other words, since I declared the variable $x with my
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
Variable rawURL is already defined in method run()
Here's the 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
}
}
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:
for (Element link : links) {
rawURL = new URL(..);
}
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?
Upvotes: 6
Views: 4860
Reputation: 13408
in Java I am trying to do something similar, but I am getting the error
Variable rawURL is already defined in method run()
The JLS explains why you get the error:
6.4. Shadowing and Obscuring
It is a compile-time error if the name of a local variable
v
is used to declare a new variable within the scope ofv
, unless the new variable is declared within a class whose declaration is within the scope ofv
.
and to explain what the scope of v
is look at an answer where I explain it.
Basically, if you compare it to Perl, the scope is "one way" and not "two way".
void method1() {
int x;
{
int y = x; // O.K. - x is in scope.
int x = y; // Not O.K. - x is in scope and was already declared.
}
x = y; // Not O.K. - y is not in scope.
}
What you can do, it this:
void method2() {
int x;
class InsideClass {
{
int y = x;
int x = y;
}
}
}
but it's probably an overkill for your needs.
Upvotes: 1
Reputation: 294
In java you have public and private access modifiers to control the accessibility of variables however if you declare a variable in the class it would be a global variable and the one with in the class method would be local compared to the one that is outside the method. There are no absolute global and local variables you have to declare and define new variables names for new variables.
public class abc{
public int a; //global and accessible by every class outside this class too
private int b; //global and accessible only within this class
private void m(){
int x; //local and accessible only within this method
}
}
Plus you can do this
public void m(){
for(;;){
int a;
}
for(;;){
int a;
}
}
But you cannot do this
public void m(){
int a;
for(;;){
int a;
}
}
Hope this helps you out.
Upvotes: 1
Reputation: 2968
You must define a new local variable with a new name because scope of variable is like: current block + subblocks. So in your case, rawURL is defined for what you called "global" and it is visible to subblocks, ie into the for block.
In java, you can do :
{
String myvar = "";
}
{
String myvar = "";
}
because here, there are two different blocks at same level
but you can't do :
String myvar = "";
{
String myvar = "";
}
because there is conflict with same name for two variables in same block (the subblock).
Upvotes: 4