0

Scopes In Java

Hey, ive been coding in java for a long time BUT this "topic" always made me confused. how does the following code work, and the one after doesnt? I would also like a wide explanation of the following topic & when to initialize variables inside or outside a for/while loop / if statements etc.. code 1 (works): int x; if (true){ x = 1; }else{ x = 0; } System.out.println(x); code 2(doesnt work): int i; for (int j = 0; j < 6; j++){ i = 3; } System.out.println(i);

15th May 2021, 10:01 PM
Yahel
Yahel - avatar
8 Réponses
+ 3
Maybe not that much a "wide" explanation, but hopefully it makes sense. In first snippet, variable <x> was defined before, and outside of conditional `if...else` block. Variables defined in outer block scope are recognisable inside inner block(s). Variables defined in inner block(s) scope are only recognisable in the respective block. In second snippet, actually it would also work, if you initialized variable <i> as it was defined e.g. int i = 0; I guess the problem here is that, an `if...else` block has covered all the possibilities that may arise, there are assignment for <x> whether the conditional branches to the `if` block or the `else` block. This isn't the case with a for-loop. Think what happens when the loop condition never satisfies and the loop never even start? there is no code in anticipation for that possibility, it means there is a possibility that <i> will remain be uninitialized. That's probably why Java asked you to initialize variable <i> in the second snippet.
16th May 2021, 3:09 AM
Ipang
+ 2
Ipang & 🅰🅹 🅐🅝🅐🅝🅣 Got it :) Thank you for the great explanations. But in the case where I used the "if statement", even if I dont have the "else" - the code still works. even if it doesn't cover all the possibilities.
16th May 2021, 7:34 AM
Yahel
Yahel - avatar
+ 2
Yahel, The first snippet works because we used a constant expression `true` in the `if` conditional. Try to use a non-constant evaluable expression, for example by adding a variable <y> int x, y = 5; if( y < 3 ) { x = 1; } // notice here no else block You'll see Java give us the same warning to initialize <x> because a block to handle another possibility doesn't exist. Use of `true` in the conditional will direct execution flow into the `if` block no matter what. Meaning for below conditional statement ... if( true ) The `else` block (where exists), will never be reached, because the execution flow will enter the `if` block. I think an `else` block is not even necessary for such conditional because `true` will always be evaluated as truthy result. Good question 👍
16th May 2021, 10:00 AM
Ipang
+ 1
Ipang Thanks! Makes sense after all... :)
16th May 2021, 10:07 AM
Yahel
Yahel - avatar
+ 1
You're very welcome Yahel 👌
16th May 2021, 10:11 AM
Ipang
0
Yahel I hope this explanation will help you: int x; //this will work because compiler know atleast one case will be execute either if block or else block if (true) x = 0; else x = 1; System.out.println(x); int i;//here would be compile time error because compiler does not know that for loop will work as we can see it will work but how compiler will know so compiler raise error to initialise variable i for (int j = 0; j < 6; j++) { i = 3; } System.out.println(i);
16th May 2021, 5:32 AM
A͢J
A͢J - avatar
- 1
Yahel if works without else because compiler know true will be always true so if will execute
16th May 2021, 10:58 AM
A͢J
A͢J - avatar
- 1
Yahel If have initialised any collection and you are checking not null then doens't make sense because collection is not empty so it would never be null. For example: List<Long> list = new ArrayList<Long>(); if (list != null) { //here list never would be null } Same like this: true will be always true. One more example : while(true) { //this loop will run forever because true is already true }
16th May 2021, 11:04 AM
A͢J
A͢J - avatar