w3resource

Using Static Blocks in Java: Initializer Class Example

Java Static Members: Exercise-3 with Solution

Static Block
Write a Java program to create a class called "Initializer" with a static block that initializes a static variable 'initialValue' to 1000. Print the value of 'initialValue' before and after creating an instance of "Initializer".

Sample Solution:

Java Code:

Initializer.java

// Define the Initializer class
public class Initializer {
    // Declare a static variable initialValue
    static int initialValue;

    // Static block to initialize initialValue
    static {
        initialValue = 1000;
        System.out.println("Static block: initialValue initialized to " + initialValue);
    }

    // Main method to demonstrate the static block
    public static void main(String[] args) {
        // Print initialValue before creating an instance
        System.out.println("Before creating an instance: initialValue = " + Initializer.initialValue);

        // Create an instance of Initializer
        Initializer initializer = new Initializer();

        // Print initialValue after creating an instance
        System.out.println("After creating an instance: initialValue = " + Initializer.initialValue);
    }
}

Output:

Static block: initialValue initialized to 1000
Before creating an instance: initialValue = 1000
After creating an instance: initialValue = 1000

Explanation:

  • Define the Initializer class:
    • The Initializer class is defined using the class keyword.
  • Declare a static variable initialValue:
    • The static variable initialValue is declared.
  • Static block to initialize initialValue:
    • A static block is used to initialize initialValue to 1000.
    • A message is printed within the static block to indicate the initialization of initialValue.
  • Main method to demonstrate the static block:
    • The main method is defined to test the Initializer class.
    • The value of initialValue is printed before creating an instance of the Initializer class.
    • An instance of the Initializer class is created.
    • The value of initialValue is printed after creating an instance of the Initializer class.

Note on Java Static Members:

In the above exercise, the static members work by:

  • Static Variable: The static variable initialValue is shared across all instances of the Initializer class. It is initialized once when the class is loaded.
  • Static Block: The static block is executed when the class is loaded into memory, setting the value of initialValue to 1000. This ensures that the static variable is initialized before any instances of the class are created or any static methods are called.
  • Main Method: The main method demonstrates the static block by printing the value of initialValue before and after creating an instance of the Initializer class. This shows that the static block is executed only once, and the value of the static variable remains the same before and after instance creation.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Static Members Previous: Using Static Methods in Java: MathUtility Class Example.
Java Static Members Next: Using Static Final Variables in Java: Constants Class Example.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-3.php