guna ker
guna ker

Reputation: 49

Why can't we declare variables in if statement?

I know that there is a question like this on Stack Overflow but I want to know why we can't declare variables inside if statements so that we can save space by just utilizing the if scope.

For example, if I write:

if (int i) {
    ...
} 

then I could use i only within the if scope, but if I write it outside of the if statement, then i variable should be in memory for the whole block.

Upvotes: -38

Views: 2235

Answers (5)

mikeb
mikeb

Reputation: 11287

An if statement is a test, so declaring a variable in an if does not make any sense.

Think about it, an if is for something like this:

if(a == 1){
  // blan
}

If you declare a in the if condition, you are essentially comparing 2 static values.

Lots of languages let you declare an "iterator" variable in a for loop:

for(int a = 0 ; a < somelist.length ; a++){
  // Do something
}

// a is out of scope here

Upvotes: 3

Ren&#233; Link
Ren&#233; Link

Reputation: 51463

why can't we declare variables in the if statement?

Because the Java Language Specification does not allow it.

if I write if(int i){} then I could use i only for if the scope

You can use blocks

public void someMethod() {
  {
    int i = 1; // visible only to the current block
  } {
    int i = 0; // visible only to the current block
    if (i == 0) {
      System.out.println("i = " + i);
    }
  }

  // compiler error, because i is not visible outside the block
  System.out.println(i);
}

But this decreases the readability of your code. So I would recommend to NOT use it.

Upvotes: 4

Stultuske
Stultuske

Reputation: 9437

if ( int i ) => int i is not a boolean expression, so this won't work.

if ( booleanExpr ){
  int a = 5;
...
}

here a is a local variable within the if block. At the end of the if-block, it is removed from the stack and becomes unusable.

int a = 5;
if ( booleanExpr){
  ...
}

Here a is not local to the if block, but to the containing method. So, it will be on the stack during the entire execution of the method (starting from its declaration).

a could also be an instance or static variable to the class, then it's accessible in every method of the class (instance variables are not accessible in a static context, though)

Upvotes: 8

Serban Stoenescu
Serban Stoenescu

Reputation: 3886

You can restrict the scope of your variable to make it visible only in the if statement like this:

System.out.println("Hello World!");
//use a codeblock to restrict access
{
  int i = 4;
  if(i!=0)
  {
    System.out.println("i = "+i);// this is OK
  }
}
System.out.println("i = "+i);//this is not OK

Upvotes: 3

David
David

Reputation: 219037

why can't language support it

That's the wrong question to ask. The real question is:

Is there a compelling reason for the language to support it?

That is, is there a compelling reason to implement it, exhaustively test it for all possible edge cases, and maintain it in all future versions of the language?

In this case, no. There isn't. It might be handy in a small number of cases, but it's not a core feature of the language and isn't really necessary. So there's no compelling reason to make the implementation of the language more complex and incur significant cost now and well into the future to support it.

Upvotes: 0

Related Questions