Java Static Initializer Block

Sort by

recency

|

1445 Discussions

|

  • + 0 comments

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

    public class Solution {

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

    }

  • + 0 comments

    import java.util.*;

    public class Solution {

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

    }

  • + 0 comments

    Java is such a versatile and powerful language—its “write once, run anywhere” philosophy has made it a cornerstone in everything from enterprise systems to Android apps. Mostbet Login ID and Password

  • + 0 comments

    Here is Java Static Initializer Block solution - https://programmingoneonone.com/hackerrank-java-static-initializer-block-solution.html

  • + 0 comments

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

    public class Solution {

    static int B, H;
    static boolean flag = true;
    
    // Static block to initialize and validate B and 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");
        }
        sc.close();
    }
    
    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) {
            int area = B * H;
            System.out.println(area);
        }
    }
    

    } **