Java Static Initializer Block

  • + 1 comment

    After reading this, I just realized that the whole point of using static is that this line below are running before the main class is called (I made the code more readable here below:)

    static boolean flag = true;
    static int B,H;
    
    static{
        Scanner s=new Scanner(System.in);
    		B=s.nextInt();
    		s.nextLine();
    		H=s.nextInt();
    		s.close();
    		if(H<=0 || B<=0) {
            flag=false;
            System.out.println("java.lang.Exception: Breadth and height must be positive");
        }
    }