-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStaticInitialization.java
44 lines (34 loc) · 1.07 KB
/
StaticInitialization.java
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
package com.javaexperiments;
import java.util.Scanner;
public class StaticInitialization {
private static int B;
private static int H;
private static boolean flag = false;
static {
System.out.println("static block can be used to initialize singleton instance, " +
"to prevent using synchronized getInstance() method.");
System.out.println("static initialization blocks allows more complex initialization.");
System.out.println("\n");
Scanner input = new Scanner(System.in);
B = input.nextInt();
input.nextLine();
H = input.nextInt();
input.close();
try {
if (B <= 0 || H <= 0) {
throw new Exception("java.lang.Exception: Breadth and height must be positive");
} else {
flag = true;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
if(flag){
int area=B*H;
System.out.print(area);
}
}
}