Why can't variables be declared in an if state

2019-01-23 14:21发布

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.)

标签: java scope
13条回答
姐就是有狂的资本
2楼-- · 2019-01-23 15:01

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-23 15:03

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.

查看更多
The star\"
4楼-- · 2019-01-23 15:03

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.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-23 15:03

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'.

查看更多
Anthone
6楼-- · 2019-01-23 15:10
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;
    }
查看更多
冷血范
7楼-- · 2019-01-23 15:11

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

查看更多
登录 后发表回答