25

The following Java code does not compile.

int a = 0;

if(a == 1) {
    int b = 0;
}

if(a == 1) {
    b = 1;
}

Why? There can be no code path leading to the program assigning 1 to b without declaring it first.

It occurred to me that b's variable scope might be limited to the first if statement, but then I wouldn't understand why. What if I really don't want to declare b needlessly, in order to improve performance? I don't like having variables left unused after declaration.

(You may want to argue than I could simply declare b in the second if statement, in that case, just imagine that it could be in a loop somewhere else.)

4

13 回答 13

33

Variables can be declared inside a conditional statement. However you try and access b in a different scope.

When you declare b here:

if(a == 1) {
    int b = 0;
}

It is only in scope until the end }.

Therefore when you come to this line:

b = 1;

b does not exist.

于 2013-07-03T09:13:29.113 回答
12

Why? There can be no code path leading to the program assigning 1 to b without declaring it first.

You are right, but the compiler doesn't know that. The compiler does not execute the code. The compiler only translates to bytecode without evaluating expressions.

于 2013-07-03T11:11:21.850 回答
6

This { } defines a block scope. Anything declared between {} is local to that block. That means that you can't use them outside of the block. However Java disallows hiding a name in the outer block by a name in the inner one. This is what JLS says :

The scope of a local variable declaration in a block (§14.2) is the rest of the block in which the declaration appears, starting with its own initializer (§14.4) and including any further declarators to the right in the local variable declaration statement.

The name of a local variable v may not be redeclared as a local variable of the directly enclosing method, constructor or initializer block within the scope of v, or a compile-time error occurs.

于 2013-07-03T09:14:29.777 回答
3

Its all about java variable scoping.

You'll need to define the variable outside of the if statement to be able to use it outside.

int a = 0;
int b = 0;

if(a == 1) {
    b = 1;
}

if(a == 1) {
    b = 2;
}

See Blocks and Statements

于 2013-07-03T09:14:00.267 回答
2

It is a local variable and is limited to the {} scope.

Try this here.

于 2013-07-03T09:16:43.217 回答
2

you have declared b variable inside if block that is not accessible out side the if block and if you want to access then put outside if block

于 2013-07-03T09:17:35.953 回答
2

The scope of b is the block it is declared in, that is, the first if. Why is that so? Because this scoping rule (lexical scoping) is easy to understand, easy to implement, and follows the principle of least surprise.

If b were to be visible in the second if:

  • the compiler would have to infer equivalent if branches and merge them to a single scope (hard to implement);
  • changing a condition in a random if statement would potentially make some variables visible and others hidden (hard to understand and source of surprising bugs).

No sane language has such a complicated scoping rule.

w.r.t. performance - declaring an extra variable has a negligible impact on performance. Trust the compiler! It will allocate registers efficiently.

于 2013-07-03T09:24:52.880 回答
1

Because when b goes out of scope in the first if (a == 1) then it will be removed and no longer exists on the stack and therefore can not be used in the next if statement.

于 2013-07-03T09:14:04.640 回答
1

{ } is used to define scope of variables.And here you declared :

if(a == 1) 
{
    int b = 0;
}

So here scope of b will be only in { }.So you are using variable b outside { }, it is giving compilation error.

You can also refer this:

http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

于 2013-07-03T09:17:14.623 回答
1

If you re declaring variable inside a block then the limitation of the variable limits to the particular block in which it got declared.

NOTE : Only static variables has access from anywhere in the program.

In you code :

int a = 0;

if(a == 1) {
    int b = 0;
}

if(a == 1) {
    b = 1;
}

variable 'a' can be accessed in any if statement as its declare outside the block but, variable 'b' is declare inside if hence limited its use outside the block.

If you want to use 'b' outside the if statement you have to declare it where you have declare variable 'a'.

于 2013-07-03T09:21:29.413 回答
1
int a = 0;

if(a == 1) {
int b = 0; // this int b is only visible within this if statement only(Scope)
}

if(a == 1) {
b = 1; // here b can't be identify
}

you have to do following way to make correct the error

    int a = 0;
    int b = 0;
    if(a == 1) {
       b=0;
    }
    if(a == 1) {
        b = 1;
    }
于 2013-07-03T09:22:53.397 回答
1

Just for completeness sake: this one works as well (explanation is scoping, see the other answers)

int a = 0;

if(a == 1) {
    int b = 0;
}

if(a == 1) {
    int b = 1;
}

Due to scoping, b will only be accessible inside the if statements. What we have here are actually two variables, each of which is just accessible in their scope.

于 2013-07-03T09:42:45.910 回答
0

variable b's scope is only until the if block completes, as this is where you declared the variable. That is why it cannot be accessed on the following block. This is for memory allocation, otherwise they would be ALOT of variables floating around in the memory.

int a = 0;

if(a == 1) {
   int b = 0;    
} //b scope ends here

if(a == 1) {
    b = 1; //compiler error
}
于 2013-07-04T09:20:47.553 回答