Java Static Initializer Block

  • + 1 comment

    I tried doing it that way but it went wrong, so I had to take the:

    Scanner scan = new Scanner(System.in);
    
    B = scan.nextInt();
    
    H = scan.nextInt();
    

    out of the block.

    Why is it?

    • + 1 comment

      No You can't do that because we need to reference the variables in main method,as main method is non-editable.so the only option you have that while compiling, when JVM loads (.class) file in the memory intialize all those variables at the same time that can be done when you declare variables as static.

      • + 0 comments

        Thanks, that helps a lot to understand how it works