Skip to content

Commit e4b93dc

Browse files
committed
feat: add StaticMember example demonstrating static variables, methods, and blocks
Implemented StaticMember.java to illustrate Java's static keyword usage. Includes a static variable initialized via a static block and modified through a static method, showcasing class-level data sharing and lifecycle. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8218980 commit e4b93dc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class StaticMember {
2+
// Static variable
3+
static int count = 0; // Static variable initialized to 0
4+
5+
// Static method
6+
public static void increment() {
7+
count++; // Static method to increment the count
8+
}
9+
10+
// Static block
11+
static {
12+
count = 10; // Static block initializes the static variable 'count' to 10
13+
}
14+
15+
public static void main(String[] args) {
16+
// Calling the static method
17+
StaticMember.increment(); // Calling the increment method to increase count by 1
18+
19+
// Printing the static variable
20+
System.out.println("Count: " + count); // Prints the value of count, which is now 11
21+
}
22+
}

0 commit comments

Comments
 (0)