Java Static Initializer Block

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        static int a;
        static int b;
    
        static {
            Scanner sc = new Scanner(System.in);
            a = sc.nextInt();
            b = sc.nextInt();
            sc.close();
            if (a <= 0 || b <= 0) {
                System.out.println("java.lang.Exception: Breadth and height must be positive");
                throw new RuntimeException("numero negativo");
            }
        }
    
        public static void main(String[] args) {
            int area = a * b;
            System.out.println(area);
        }
    }