-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatic_Initializer_Block.java
More file actions
56 lines (51 loc) · 1.44 KB
/
Static_Initializer_Block.java
File metadata and controls
56 lines (51 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution
{
//Write your code here
static Scanner sc = new Scanner(System.in); //assigning this also as static because with the help of the reference variable value is provided to B and H..
static int B; //value of breadth..
static int H; //value of length..
//alternative method of using static initializer block...
public static boolean flag = initializeClassVariable();
private static boolean initializeClassVariable()
{
B = sc.nextInt();
H = sc.nextInt();
if(B<=0 || H<=0) //if any of them is less than zero...
{
System.out.println("java.lang.Exception: Breadth and height must be positive");
System.exit(0); //when any value is negative then simply terminate the program elseee find the area of the parallelogram..
}
return true;
}
/////////////////////////////////////Another way to do this..
static Scanner sc = new Scanner(System.in);
static int B = sc.nextInt();
static int H = sc.nextInt();
static boolean flag = false;
//just use static block declaration..
static
{
if(B<=0 || H<=0)
{
System.out.println("");
flag=false;
}
else
{
flag=true;
}
}
public static void main(String[] args)
{
if(flag) //flag variable is accessed directly as it is static..no need to create an object..
{
int area=B*H;
System.out.print(area);
}
}//end of main
}//end of class