Static Block in Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Static blocks in Java, often referred to as static initialization blocks, are a feature that allows for the initialization of static variables when the class is first loaded. These blocks are executed only once: the first time the class is loaded into memory. They are typically used to perform complex initialization that cannot be handled by a single expression or to set up resources required by the class (e.g., opening a file or database connection).
Understanding Static Blocks
A static block is declared by using the static keyword followed by a pair of braces {}. Anything inside these braces is executed statically - that is, without needing to create an instance of the class. Here's a basic example:
In this example, the static block initializes an array when the class is loaded. Static blocks are executed in the order they appear in the class file, and before any static method or instance method is invoked or an instance of the class is created.
Key Uses of Static Blocks
Static initialization blocks are particularly useful when initial setup is necessary before any object creations or method calls are made. They can be used to:
- Initialize complex static objects.
- Set up error handling or logging frameworks that are used by the class.
- Create or configure static resources such as database connections.
Execution Order and Rules
It is important to note that static blocks and static variables are processed in the order they appear in the class, from the top to the bottom. If a static variable is referenced later in the class before it has been initialized, it will not have the expected value.
Comparing To Constructors
While constructors are meant for initializing instances of a class, static blocks initialize static data, or perform actions that are general to all instances. This preparation happens independently of any specific object instantiation, thus making static blocks suitable for one-time initializations that apply to all instances of the class.
Example Demonstrating Usage
Consider a class that needs to load configuration settings from a file when the class is first used:
In this example, the static block loads configuration details into a Properties object when the AppConfig class is loaded. This ensures that the configuration is available before any instances are created or any static methods are called.
Edge Cases and Considerations
- Error Handling: If a static initialization block throws an exception, the class might not be loaded properly. This can lead to a
ExceptionInInitializerError. - Performance Considerations: Since static blocks are executed only once, they should not contain operations that need to be repeated unless they are specifically purposed to set up static data.
Summary Table
| Feature | Description |
| Initialization | Executes when class is first loaded |
| Usage | Ideal for one-time setup operations |
| Execution Frequency | Once (when the class is first loaded) |
| Comparison to Constructors | Static blocks initialize class-wide resources |
| Typical Use Cases | Loading configurations, setting up static resources |
| Error Handling in Static Blocks | Possible ExceptionInInitializerError if block throws an exception |
In summary, static blocks in Java provide a powerful mechanism for initializing static data, setting up configurations, or ensuring certain tasks are performed when a class is first loaded. They introduce a structured way to manage static initialization, separate from instance-specific construction handled by constructors.
Related reading
- Static code blocks
- Static method behavior in multi-threaded environment in java
- Static method in a generic class?
- Static nested class in Java, why?
- Still getting warning Configuration 'compile' is obsolete and has been replaced with 'implementation
- Stop a Kafka Streams app
- stop IntelliJ IDEA to switch java language level every time the pom is reloaded or change the default project language level
- Store an ordering of Enums in Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.