0

I understand that a block defines a scope of a variable. And empty blocks inside a method are for setting scope. But why are empty blocks inside methods initialising variables as well unlike in blocks used with loops etc.

class A{
    public static void main(String args[]){
        int a;
        int b:
        {
            a = 10;
        }
        for(int i = 0; i < 1; i++){
            b = 20;
        }
        System.out.println(b); //error here
        System.out.println(a);
        // doesnt give error and prints 10. why?
    }
}

My question is : why are properties of an empty block inside a method not similar to blocks used with loops or conditional blocks etc

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • The non empty block with no loop or condition can be trivially determined to always execute. While you can see your `for` loop must run at least once, there is no what to determine this described in the JLS. – Peter Lawrey Oct 04 '18 at 17:24
  • This is answered here: [Initialization Blocks](https://stackoverflow.com/questions/2420389/static-initialization-blocks) – CoupFlu Oct 04 '18 at 18:06
  • @JamieSnipes thankyou..but my question isnt about static initialization blocks. I was confused with the properties of empty blocks inside any type of method. I understand it now. –  Oct 04 '18 at 18:15

3 Answers3

2

Because that block for a will be executed anyway since there are no enclosing operators. But b is initialized inside loop so compiler sees that b = 20 is executed only inside loop and if loop is not executed that b is not initialized. And compiler doesn't run your code to understand that there are no such code paths where loop is no executed.

Ivan
  • 8,508
  • 2
  • 19
  • 30
0

Order of execution.

See the article here:

Order of Execution

CoupFlu
  • 311
  • 4
  • 20
  • That is not initializing block. That is a usual block of code inside method. – Ivan Oct 04 '18 at 17:11
  • Also in the link provided, it mentions initialisation blocks are executed when objects are created. And it is mentioned separately from an "instance initialisation block". But the empty block inside a method is not executed when objects are created unless the method is explicitly invoked. Isnt it. Kindly correct me if am wrong. –  Oct 04 '18 at 17:23
  • Your main method has a static initialization block {a=10;}, A is initialized. B is not and may never be from the Compiler standpoint. – CoupFlu Oct 04 '18 at 17:28
  • {a=10;} inside main isnt called a static initialization block i suppose. This is just an empty block inside a static method.isnt it completely different from a static block? –  Oct 04 '18 at 17:33
  • static init block is a piece of code starting with `static` and inside brackets and in placed inside class declaration but outside of any method like `public class A{ static {System.out.println("Static block");} }`. Static init block can access only static variables. And in the question both `a` and `b` are local variables inside method. – Ivan Oct 04 '18 at 19:09
  • so are we calling it a non-static block like in the links I provided? – CoupFlu Oct 04 '18 at 19:13
  • It is not `initialization block`. It is simple code block like inside for loop or if statement. – Ivan Oct 04 '18 at 19:17
  • Now I'm confused, because one is checked by the compiler and one is not. – CoupFlu Oct 04 '18 at 19:20
  • Just check my answer. For one code block compiler sees that it is executed 100% and another is executed inside loop which may not have iterations. – Ivan Oct 04 '18 at 19:31
0

I suspect you are getting a compile and not a runtime error. It's assuming b is never initialized because the compiler assumes the for loop may not execute.

You should always set your variables to a default value, just in case.

  • 2
    Your last statement is not true. There are times when you want to leave the variable uninitialized, to be certain all possible code paths set it to a value. – VGR Oct 04 '18 at 17:17
  • Yes. Now I get it. Thankyou –  Oct 04 '18 at 17:18