Using Static Blocks for Complex Initialization in Java: Example
Java Static Members: Exercise-7 with Solution
Static Block for Complex Initialization
Write a Java program to create a class called "ComplexInitializer" with a static block that initializes multiple static variables (x, y, z) using complex logic. Print the values of these variables in the main method.
Sample Solution:
Java Code:
ComplexInitializer.java
// Define the ComplexInitializer class
public class ComplexInitializer {
// Declare static variables x, y, z
private static int x;
private static int y;
private static int z;
// Static block to initialize the static variables using complex logic
static {
x = 10;
y = 20;
z = calculateZ(x, y);
}
// Static method to calculate the value of z
private static int calculateZ(int a, int b) {
return a * b + (a - b);
}
// Main method to print the values of the static variables
public static void main(String[] args) {
System.out.println("Value of x: " + x);
System.out.println("Value of y: " + y);
System.out.println("Value of z: " + z);
}
}
Output:
Value of x: 10 Value of y: 20 Value of z: 190
Explanation:
- Define the ComplexInitializer class:
- The ComplexInitializer class is defined using the class keyword.
- Declare static variables x, y, z:
- The static variables x, y, and z are declared within the class.
- Static block to initialize the static variables using complex logic:
- The static block is used to initialize the static variables. It sets x to 10, y to 20, and calculates z using the calculateZ method.
- The static block runs once when the class is first loaded, ensuring that the static variables are initialized before they are used.
- Static method to calculate the value of z:
- The calculateZ method is a static method that takes two integers as parameters and returns the result of a complex calculation involving multiplication and subtraction.
- This method is used within the static block to initialize the value of z.
- Main method to print the values of the static variables:
- The main method is defined to test the ComplexInitializer class.
- It prints the values of x, y, and z to the console.
Note on Java Static Members:
In the above exercise, the static members work by:
- Static Block: The static block is used to initialize multiple static variables (x, y, and z) with complex logic. It ensures that these variables are initialized once when the class is loaded, making the initialization efficient and centralized.
- Static Variables: The static variables x, y, and z are shared among all instances of the ComplexInitializer class (if any were created). They are initialized using the static block, which uses both direct assignments and a static method to perform complex calculations.
- Static Method: The static method calculateZ is used within the static block to perform a complex calculation. It demonstrates how static methods can be used to encapsulate logic that aids in the initialization of static variables.
Java Code Editor:
Improve this sample solution and post your code through Disqus.
Java Static Members Previous: Using Static Methods and Variables in Java: IDGenerator Example.
Java Static Members Next: Understanding Static Members in Java: BankAccount Class Example.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/static_members/java-static-members-exercise-7.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics