Java Static Initializer Block

  • + 7 comments

    static boolean flag=true;

    static int B,H;

    static {

    Scanner scan = new Scanner(System.in);
    
    B = scan.nextInt();
    
    H = scan.nextInt();
    
    try{   
    
    if(B<=0 || H<=0){
    
    flag = false;
    
    throw new Exception("Breadth and height must be positive");
    
    } 
    
    }catch(Exception e){
    
    System.out.println(e);
    
    }
    

    }

    • + 1 comment

      Instead of using or you can use multiplication i.e, if bh<0 the string is printed. This is better because, if we extend the code this format will be better to debug.

      • + 1 comment

        we should not use bh<0 condition because if b and h both are negative, then your code will set flag as true which is incorrect

        • + 1 comment

          we cant use this as if both b & h are negative the condition will get true which should not be happen

          • + 3 comments

            image

            if both b & h are negative the condition will get false and it throw the exception

            • + 0 comments

              why do we need flag in this to true can we use and operator insted of or .................................please help

            • + 0 comments

              I found twice repeating "java.lang.Exception:" in your code. Line 17 should be corrected as : throw new Exception("Breadth and height must be positive");

            • + 0 comments

              static int B,H; static boolean flag = true; static Scanner scan = new Scanner(System.in); static{ B = scan.nextInt(); scan.nextLine(); H = scan.nextInt(); scan.close();

              if(B<= 0 || H<=0){
                  flag = false;
                  System.out.println("java.lang.Exception: Breadth and height must be positive");
              }
              

              }

              This works fine.

    • + 1 comment

      Code is working but how you are calculating the area .

      • + 2 comments

        Area is calculated in the main method. Expand the code and you will find it! :)

        • + 0 comments

          thank you

        • + 0 comments

          cool...

    • + 1 comment

      Why can't we put boolean flag = true; and int B,H; in the static block??

      • + 5 comments

        No You can't do that because we need to reference the variables in main method,as main method is non-editable.so the only option you have that while compiling, when JVM loads (.class) file in the memory intialize all those variables at the same time that can be done when you declare variables as static.

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

        Note: variables inside the static block is not static.

        • + 1 comment

          why are we using the boolean variable flag?

          • + 0 comments

            alredy given flag in main function as a conditon for if so we should use flag

        • + 1 comment

          how to access the main class??

          • + 0 comments

            Click on arrow you will find the full part of main class.

        • + 3 comments

          i have used the same code, still getting errors...

          .java:36: error: class, interface, or enum expected public static void main(String[] args){ ^ .java:39: error: class, interface, or enum expected System.out.print(area); ^ .java:40: error: class, interface, or enum expected } ^

          I'm not getting this error...plz help me out.

          • + 1 comment

            System.out.print(area); ?????

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

            paste the same code,complied and tested and let me know!

            • + 1 comment

              i did the same thing but got an error class,enum or interface expected.pls help me

              • + 0 comments

                i also did exactly same but i am getting error

          • + 1 comment

            Do not close class before main. There is closing parenthesis for class after main.

            • + 0 comments

              Thanks a ton! It Worked for me.

        • + 0 comments

          its showing semicolan expected

        • + 0 comments

          thank u

    • + 1 comment

      I tried doing it that way but it went wrong, so I had to take the:

      Scanner scan = new Scanner(System.in);
      
      B = scan.nextInt();
      
      H = scan.nextInt();
      

      out of the block.

      Why is it?

      • + 1 comment

        No You can't do that because we need to reference the variables in main method,as main method is non-editable.so the only option you have that while compiling, when JVM loads (.class) file in the memory intialize all those variables at the same time that can be done when you declare variables as static.

        • + 0 comments

          Thanks, that helps a lot to understand how it works

    • + 0 comments

      It works vs this static Scanner input = new Scanner(System.in); static boolean flag = true; static int B = input.nextInt(); static int H = input.nextInt();

      static{ try{ if(B <= 0 || H <= 0){ flag = false; throw new Exception("Breadth and height must be positive"); } }catch(Exception e){ System.out.println(e); }

      }

    • + 0 comments

      end class erorr

    • + 0 comments

      why are we defining the B and H two times like first in the starting with static keyword and again in scanner? scan.nextInt() is also saying the same thing that B is of integer type, Please explain