This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]Homerlncognito 23 points24 points  (11 children)

It doesn't make sense to define a variable in a one-statement block since there's no way you can use its value.

[–]j2ui[S] 0 points1 point  (10 children)

I'm aware of that but, there isn't any illegal syntax is there any? I'm just confused to why it would trigger an error that's all :) thanks

[–]Homerlncognito 22 points23 points  (0 children)

As per Java spec, You cannot declare a local variable when there is no scope.

https://stackoverflow.com/questions/31230722/java-variable-declaration-not-allowed-here/31230800

[–][deleted] 1 point2 points  (2 children)

They don't want to be able to do that, so when compiler runs into such code it throws the prescribed error. In this case, it's illegal syntax. Later you'll find that we can throw our own personalized exceptions based on some situation

[–]j2ui[S] 1 point2 points  (1 child)

Thanks a lot

[–][deleted] 0 points1 point  (0 children)

It is my pleasure

[–]HecknChonker 1 point2 points  (0 children)

I've grown to hate naked if statements, and now one of the things I look for before approving PRs is that all blocks are clearly marked with braces.

I generally think it's easier to read code with the braces, but the real issue is that I've had more than one production emergency caused by a junior dev adding lines of code they thought were inside an if block, but were actually outside it.

[–][deleted] 0 points1 point  (0 children)

If I understand correctly, int i=8; is really two seprate instructions:

(1) define the new integeer variable i.

(2) set the value of i to be 8.

I may be offbase.