Java Static Initializer Block

Sort by

recency

|

1409 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution { public static int B; public static int H; public static boolean flag; public static Scanner scan;

    static{

        scan = new Scanner(System.in);
        flag = true;
        B = scan.nextInt();
        H = scan.nextInt();
    
        if((B<=0)||(H<=0)){
            System.out.println("java.lang.Exception: Breadth and height must be positive");
            System.exit(0);
                }
        }
    

    }

    public static void main(String[] args){

  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

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

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

    }//end of main
    

    }//end of class

  • + 0 comments

    Here the Simplest way :-

    import java.io.; import java.util.;

    public class Solution {

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

    }

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        static boolean flag;
        static int B;
        static int H;
    
        static {
            Scanner sc = new Scanner(System.in);
            B = sc.nextInt();
            H = sc.nextInt();
    
            if (B <= 0 || H <= 0) {
                flag = false;
                System.out.println("java.lang.Exception: Breadth and height must be positive");
            } else {
                flag = true;
            }
        }
    
        public static void main(String[] args) {
            if (flag) {
                int area = B * H;
                System.out.println(area);
            }
        }
    }
    
  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    static boolean flag=true; static int B,H; static{ Scanner sc = new Scanner(System.in); B=sc.nextInt(); H=sc.nextInt(); try{ if(B<0||H<0) throw new Exception("Breadth and height must be positive"); }catch(Exception e){ System.out.print(e); flag=false; } } public static void main(String[] args){ if(flag){ int area=B*H; System.out.print(area); }

    }//end of main
    

    }//end of class