-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticBlock.java
More file actions
44 lines (41 loc) · 1.42 KB
/
Copy pathStaticBlock.java
File metadata and controls
44 lines (41 loc) · 1.42 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
/**
* * This file is part of the Java Programming Lecture project.
* *
* * A static block is executed once when the class is loaded
* * into memory—before main() or any object is created.
* * It is ideal for
* * - Initializing static variables
* * - Loading configuration files or resources
* * - Performing setup tasks that should run only once
* *
* * This Java snippet illustrates:
* * - Declares 2 static variables: radius and area.
* * - These belong to the class, not to any instance.
* * - As soon as the class is loaded into memory, static block runs.
* * - It sets radius = 4 and calculates area = 16.
* * - The message "Invoked java static block!" confirms the block's execution
* *
* @package II_Unit.Static_Keyword;
* @author Dr. S. Sampath Kumar
* @since 11-08-2025
* @version 1.0
*/
//package II_Unit.Static_Keyword;
public class StaticBlock {
// static variable declaration
static int area;
static int radius;
// run before main()
// will run only once.
// class‑level initialization
static {// static block declaration
System.out.println("Invoked java static block!");
// initializing radius and area static variable
radius = 4;
area = radius * radius;
}
public static void main(String[] args) {
System.out.println("In the main() function");
System.out.println("Area of square with radius " + radius + ": " + area);
}
}