Skip to content

Commit ceeea4f

Browse files
committed
PR review changes: Added more scenarios to the static variable test
Covered the following cases: 1. Variable initialization in a static block 2. Variable initialization in a static nested class
1 parent 2ba3b4f commit ceeea4f

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

core-java-modules/core-java-lang-3/src/main/java/com/baeldung/staticvariables/StaticVariableDemo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@
33
public class StaticVariableDemo {
44
public static int i;
55
public static int j = 20;
6+
public static int z;
7+
8+
static {
9+
z = 30;
10+
a = 40;
11+
}
12+
13+
public static int a = 50;
14+
15+
public static final int b = 100;
616

717
public StaticVariableDemo() {
818
}
19+
20+
static class Nested {
21+
public static String nestedClassStaticVariable = "test";
22+
}
923
}

core-java-modules/core-java-lang-3/src/test/java/com/baeldung/staticvariables/StaticVariableUnitTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,81 @@ public void initializeStaticVariable_checkAssignedValues() {
3333
}
3434

3535
}
36+
37+
@Test
38+
public void initializeStaticVariable_checkStaticBlock() {
39+
40+
try {
41+
Class<?> staticVariableDemo = this.getClass()
42+
.getClassLoader()
43+
.loadClass("com.baeldung.staticvariables.StaticVariableDemo");
44+
45+
Field field1 = staticVariableDemo.getField("z");
46+
47+
assertThat(field1.getInt(staticVariableDemo)).isEqualTo(30);
48+
49+
Field field2 = staticVariableDemo.getField("a");
50+
51+
assertThat(field2.getInt(staticVariableDemo)).isEqualTo(50);
52+
53+
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {
54+
e.printStackTrace();
55+
} catch (IllegalArgumentException e) {
56+
e.printStackTrace();
57+
} catch (IllegalAccessException e) {
58+
e.printStackTrace();
59+
}
60+
61+
}
62+
63+
@Test
64+
public void initializeStaticVariable_checkFinalValues() {
65+
66+
try {
67+
Class<?> staticVariableDemo = this.getClass()
68+
.getClassLoader()
69+
.loadClass("com.baeldung.staticvariables.StaticVariableDemo");
70+
71+
Field field1 = staticVariableDemo.getField("b");
72+
73+
assertThat(field1.getInt(staticVariableDemo)).isEqualTo(100);
74+
75+
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {
76+
e.printStackTrace();
77+
} catch (IllegalArgumentException e) {
78+
e.printStackTrace();
79+
} catch (IllegalAccessException e) {
80+
e.printStackTrace();
81+
}
82+
83+
}
84+
85+
@Test
86+
public void initializeStaticVariable_checkInnerClassValues() {
87+
88+
try {
89+
Class<?> staticVariableDemo = this.getClass()
90+
.getClassLoader()
91+
.loadClass("com.baeldung.staticvariables.StaticVariableDemo");
92+
93+
Class<?>[] nestedClasses = staticVariableDemo.getClasses();
94+
95+
for (Class<?> nestedClass : nestedClasses) {
96+
if (nestedClass.getName()
97+
.equals("Nested")) {
98+
99+
Field field1 = nestedClass.getField("nestedClassStaticVariable");
100+
assertThat(field1.get(nestedClass)).isEqualTo("test");
101+
}
102+
}
103+
104+
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {
105+
e.printStackTrace();
106+
} catch (IllegalArgumentException e) {
107+
e.printStackTrace();
108+
} catch (IllegalAccessException e) {
109+
e.printStackTrace();
110+
}
111+
112+
}
36113
}

0 commit comments

Comments
 (0)