3

Lately I started programming in Java again after a short break and whilst doing a project for a friend I noticed something odd: It appears you can not create objects in shortened ifs, example:

if( condition )
     Statement statement = con.createStatement();

(I cut the code short for simplicity's sake.)

I receive an error saying that Statement (a class from the java.sql package) can not be resolved to a variable, however, if I were to wrap the code with braces it would work fine.

I assume this is some problem with the compiler turning this in to a multiline statement but I'm uncertain, I'd like to know the reason for this behaviour, thanks in advance!

Biffen
  • 6,249
  • 6
  • 28
  • 36
Mickey695
  • 108
  • 8
  • 1
    Can you post a full eample please? – Jens Sep 06 '14 at 08:15
  • 1
    That has something to do with `statement`'s `scope`!!! Please post the relevant code to clarify more,cutting short the code won't help us! – Am_I_Helpful Sep 06 '14 at 08:16
  • Yes I am sure adding braces fixed the problem, yes I have imported the required classes. Full example: `if( ! (con == null) ) Statement statement = con.createStatement();` Gives an error whilst: `if( ! (con == null) ){ Statement statement = con.createStatement(); }` Works fine – Mickey695 Sep 06 '14 at 08:17
  • 1
    Well, I tried similar code : `if (i > 3) String str = "aaa";`. I got a different error - `error : not a statement`. – Eran Sep 06 '14 at 08:21
  • 1
    And this is why you should always use `{}` in your *if statements*. – christopher Sep 06 '14 at 08:21

2 Answers2

7

You can't declare a variable there (the current error, from Java 8, is error: variable declaration not allowed here). If you think about it, it makes sense: You haven't created a new scope (but using a block), but you're creating a situation where sometimes in the current scope, there will be a statement variable, and other times there won't. E.g.:

if (condition)
    Statement statement = con.createStatement();

// Does `statement` exist here? What would Schrodinger say?

If you use a block, it clarifies the matter: The variable exists, but only within the block.

if (condition) {
    Statement statement = con.createStatement();
    // `statement` exists here
}
// `statement` does not exist here

If you want statement to exist in the current scope, you have to separate your declaration from your initialization:

Statement statement;

if (condition)
    statement = con.createStatement();

But then you run into the issue that statement may not have been initialized. To avoid that, you have a couple of options:

Statement statement;

if (condition)
    statement = con.createStatement();
else
    statement = null;

or

Statement statement = condition ? con.createStatement() : null;

Or of course, just use the block and only use statement within it. FWIW — and this is totally up to you — I (and many style guides) recommend always using blocks, because not doing so can introduce maintenance issues when you need (inevitably!) to add a second statement to the body of the if...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

As per Java specification,

  A local variable, one of the following:
       A local variable declared in a block (§14.4)
       A local variable declared in a for statement (§14.14)

If you don't wrap the statement within braces, it immediately falls out of scope.

More info here : https://stackoverflow.com/a/9206679/978501

Community
  • 1
  • 1
Sridhar
  • 11,466
  • 5
  • 39
  • 43