I have a little difficulty understanding how the static block works
import java.io.*;
import java.util.*;
public class Solution {
static {
Scanner sc = new Scanner(System.in);
int B = sc.nextInt();
int H = sc.nextInt();
boolean flag= false;
if(B<=0 || H<=0){
flag= false;
System.out.println("java.lang.Exception: Breath and Hieght must be positive");
}
}
public static void main(String[] args){
if(flag){
int area=B*H;
System.out.print(area);
}
}
}
when I try to run it says cannot find symbol flag, B, H. Can anyone explain why?
The scope of the variable is within static block or any block for that matter. You should declare it outside the block and define it inside ur static block.