Java Static Initializer Block

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        static int B;
        static int H;
        static boolean flag;
        static{
            Scanner sc = new Scanner(System.in);
            B=sc.nextInt();
            H=sc.nextInt();
            flag = true;
            try{
                if(B<=0 || H<=0){
                   flag = false;
                   throw(new Exception("java.lang.Exception: Breadth and height must be positive"));
                }
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            if(flag){
                System.out.print(B*H);
            }
    
        }
    }