Java Static Initializer Block

Sort by

recency

|

1461 Discussions

|

  • + 0 comments

    Easy Pesy Solution******

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

    }

    public static void main(String[] args){ if(flag){ int area=B*H; System.out.print(area); }

    }//end of main
    

    }//end of class

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
    private static boolean flag;
    private static int B;
    private static int H;
    static {
        Scanner sc = new Scanner(System.in);
        B = sc.nextInt();
        H = sc.nextInt();
        flag = B>0 && H>0;
        if(!flag){
           System.out.println("java.lang.Exception: Breadth and height must be positive"); 
        }
        sc.close();
    }
    
    public static void main(String[] args){
    		if(flag){
    			int area=B*H;
    			System.out.print(area);
    		}
    		
    	}//end of main
    
    }//end of class
    
  • + 0 comments

    static{ Scanner scan = new Scanner(System.in); int b = scan.nextInt(); int h = scan.nextInt(); try{ if(b <= 0 || h <= 0) throw new Exception("Breadth and height must be positive"); System.out.println(b*h);
    }catch(Exception e){ System.out.println(e); } }

  • + 0 comments

    static int B; static int H; static boolean flag;

    static { Scanner sc = new Scanner(System.in); B = sc.nextInt(); H = sc.nextInt(); if (B > 0 && H > 0) { flag = true; } else{ flag = false; System.out.println("java.lang.Exceptiom: Breadth and height must be positive "); } }

  • + 0 comments

    public class Solution {

    static{
             Scanner in = new Scanner(System.in);
             int b = in.nextInt();
             int h = in.nextInt();
    
             if(b <= 0 || h <= 0){
                System.out.println("java.lang.Exception: Breadth and height must be positive");
             }else{
                int x = b*h;
                System.out.println(x);
             }
        }
    
    public static void main(String[] args) {
    }
    

    }