w3resource

Using Static Blocks in Java: Configuration Class Example

Java Static Members: Exercise-5 with Solution

Static Block for Initialization
Write a Java program to create a class called Configuration with a static block that initializes a static variable 'configValue' from a configuration file (simulated with a default value). Print the value of 'configValue' in the main method.

Sample Solution:

Java Code:

Configuration.java

// Define the Configuration class
public class Configuration {
    // Declare a static variable configValue
    public static String configValue;

    // Static block to initialize configValue
    static {
        // Simulating reading from a configuration file by assigning a default value
        configValue = "Default Config Value";
        System.out.println("Static block executed: configValue initialized");
    }

    // Main method to print the value of configValue
    public static void main(String[] args) {
        // Print the value of configValue
        System.out.println("Config Value: " + configValue);
    }
}

Output:

Static block executed: configValue initialized
Config Value: Default Config Value

Explanation:

  • Define the Configuration class:
    • The Configuration class is defined using the class keyword.
  • Declare a static variable configValue:
    • The static variable configValue is declared. It will hold the configuration value.
  • Static block to initialize configValue:
    • A static block is used to initialize the static variable configValue.
    • This block simulates reading from a configuration file by assigning a default value to configValue.
    • A message is printed to indicate that the static block has been executed and configValue has been initialized.
  • Main method to print the value of configValue:
    • The main method is defined to test the Configuration class.
    • It prints the value of configValue to the console.

Note on Java Static Members:

In the above exercise, the static members work by:

  • Static Variable: The static variable configValue is associated with the class Configuration, not with any instance. It can be accessed and modified by static methods and blocks.
  • Static Block: The static block is a special block of code that is executed when the class is first loaded into memory. It is used here to initialize the static variable configValue with a default value, simulating reading from a configuration file.
  • Main Method: The main method prints the value of configValue, demonstrating that the static block has successfully initialized it.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Static Members Previous: Using Static Final Variables in Java: Constants Class Example.
Java Static Members Next: Using Static Methods and Variables in Java: IDGenerator 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-5.php