-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trivial class static initialization
- Loading branch information
1 parent
58fd980
commit b71cd33
Showing
10 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
public class Dependable { | ||
static int valueA = 100; | ||
|
||
static { | ||
valueA = 200; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
public class DependsOnDependable { | ||
static int valueB = Dependable.valueA + 50; | ||
|
||
static { | ||
valueB += 100; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
public class StaticInitialization { | ||
private static final int STATIC_FINAL_FIELD = init(); | ||
private static int staticField1; | ||
private static int staticField2; | ||
private static int[] staticArray; | ||
|
||
// Static initializer block | ||
static { | ||
staticField1 = 5; // Direct static field initialization | ||
staticArray = new int[staticField1]; // Initialize array in static block | ||
for (int i = 0; i < staticArray.length; i++) { | ||
staticArray[i] = i * staticField1; | ||
} | ||
} | ||
|
||
private static int init() { | ||
return 42; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int arraySum = 0; | ||
for (int i = 0; i < staticArray.length; i++) { | ||
arraySum += staticArray[i]; | ||
} | ||
|
||
int result = STATIC_FINAL_FIELD + staticField1 + staticField2 + arraySum; | ||
} | ||
|
||
// Another static block (executed in order of appearance) | ||
static { | ||
staticField1 += 50; // Modify staticField1 | ||
staticField2 = staticField1 * 2; // Initialize staticField2 based on staticField1 | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
public class StaticInitializationUser { | ||
public static void main(String[] args) { | ||
int result = DependsOnDependable.valueB; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters