0

When I try to run this code:

public static void main(String[] args) {

    double salary = 10.1;

    if (salary >= 100.3)
        double number = salary * 10;
    else
        double number = salary * 20;

}

I get the following errors:

Syntax error on token "double", delete this token

and that both:

number cannot be resolved to a variable

Now the code is purposely bad, as it is a question where we have to state everything that's wrong with it.

As far as I understand, it's best practice to not declare the same variable twice and it's best to always use curly brackets; but why does the double number = salary * 10; and double number = salary * 20; cause an issue if curly brackets aren't used? I would've thought that would still compile and run correctly (it works if the brackets are used).

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 2
    A declaration is not allowed in an `if` statement without braces. This is because, if you can only have one statement in the `if`, a declaration is useless. The scope ends immediately. – bcsb1001 Jan 14 '18 at 16:46
  • Read this: https://stackoverflow.com/questions/38766891/is-it-possible-to-declare-a-variable-within-a-java-while-conditional – GalAbra Jan 14 '18 at 16:47
  • You can, of course, do `double number = salary >= 100.3 ? salary * 10 : salary * 20;` or even `double number = salary * (salary >= 100.3 ? 10 : 20);` – T.J. Crowder Jan 14 '18 at 16:51
  • @bcsb1001 it would not be entirely useless: `int a = someFunctionWithSideEffect(a = veryExpensiveMethod(), a);` is (otherwise) legal (perhaps surprisingly); here, `a` can be used as a temporary variable to avoid recomputing the parameter. But this isn't exactly sensible code, and if you really want to use it, you could always just wrap it in braces. Just pointing out you *could* use a variable declaration in an if to some vaguely practical end. – Andy Turner Jan 14 '18 at 17:47

1 Answers1

4

Because it violates the Java grammar. The JLS defines if statements as follows:

IfThenStatement:
    if ( Expression ) Statement

So it's expecting a Statement. Curly braces denote a Block, and a Block is a Statement. However, a variable declaration is not a Statement.1

This is intuitively sensible behaviour - if you could declare a variable here, it would be useless, as it would immediately go out of scope.


1. Confusingly, variable declarations are referred to as LocalVariableDeclarationStatements in the JLS. However, that's not a kind of Statement as far as the language grammar is concerned.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • In what sense aren't they a statement? Sure, they're not a `Statement`, but they are a `BlockStatement`. – Andy Turner Jan 14 '18 at 17:28
  • @AndyTurner - Maybe formatting my answer would help? A `BlockStatement` is not a `Statement`, and `if` is expecting a `Statement`. – Oliver Charlesworth Jan 14 '18 at 17:32
  • yes, I think that backticks would make it clearer. I think the "confusingly" here is that the spec calls it a `Statement`, even though other statement types are not included in that. – Andy Turner Jan 14 '18 at 17:34