Java Object Initialization: Order and Constructors
Understand how Java initializes objects: field defaults, initializer blocks, constructors, static initialization, and inheritance order.

Java object initialization is the sequence of steps that brings a new instance from raw memory allocation to a fully constructed state. The order in which fields are set, initializer blocks run, and constructors execute determines whether an object is valid when its constructor returns. Misunderstanding this order is a common source of subtle bugs, especially when inheritance is involved.
The Order of Field Initialization in Java
When you write new SomeClass(), the JVM allocates memory for the object and then initializes every field to its default value: 0 for numeric primitives, false for boolean, null for references. After that, field initializers and instance initializer blocks execute in the order they appear in the source code, and finally the constructor body runs.
public class OrderExample {
int a = 1; // field initializer
int b; // default 0
{
b = 2; // instance initializer block
}
OrderExample() {
a = 3; // constructor body
}
}The field a is first set to 1, then the initializer block sets b to 2, and finally the constructor changes a to 3. If you need to know the exact value at any point, you must account for this sequence. The constructor is the last step, so it can override anything set earlier.
Instance Initializer Blocks and Their Role
Instance initializer blocks are rarely necessary, but they appear when multiple constructors must share initialization logic that cannot be expressed with a simple field assignment. They run before any constructor body, in source order relative to field initializers.
public class SharedInit {
int x;
int y;
{
x = computeX();
y = computeY();
}
SharedInit() {
// x and y already set
}
SharedInit(int extra) {
x += extra;
}
}Because the block runs before either constructor, both constructors see the same initial state. This avoids duplicating setup code. However, if the initialization logic depends on constructor arguments, a block is not the right tool; you should assign fields directly in the constructor.
Static Initialization and Class Loading
Static fields and static initializer blocks run once when the class is initialized, typically on its first active use, not when an instance is created. This distinction matters for objects that rely on shared state.
public class Config {
static String env = loadEnv();
static int retries;
static {
retries = Integer.parseInt(System.getenv().getOrDefault("RETRIES", "3"));
}
}Static initialization is serialized by the JVM: only one thread executes the static initializer, and other threads wait until it completes. But if the initializer throws an exception, class initialization fails. The first attempt can throw ExceptionInInitializerError, and later attempts may fail with NoClassDefFoundError. Keep static initialization simple and avoid performing I/O or network calls there unless you are certain about the failure behavior.
Final Fields and Object Initialization
A final instance field must be assigned exactly once by the time construction completes. The assignment can happen in a field initializer, an instance initializer block, or in a constructor. A blank final field, one declared without an initializer, must be definitely assigned by the end of every constructor.
public class Point {
final int x;
final int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}The JVM provides a memory visibility guarantee for final fields: once the constructor completes, any thread that sees the object reference will also see the final field values. This is a stronger guarantee than for non-final fields, which require proper synchronization or volatile for safe publication.
Initialization Order with Inheritance
When a subclass is instantiated, the superclass is initialized first. The JVM calls the superclass constructor before executing the subclass's field initializers and constructor body. This means you cannot rely on subclass fields being initialized when a superclass constructor calls an overridable method.
class Base {
Base() {
print();
}
void print() {
System.out.println("Base");
}
}
class Derived extends Base {
String name = "Derived";
@Override
void print() {
System.out.println(name);
}
}Creating new Derived() prints null, because name has not been assigned yet when Base's constructor runs. The field initializer executes only after the superclass constructor returns. This is a classic pitfall: never call overridable methods from a constructor.
Common Pitfalls and Cost Considerations
Object initialization in Java is fast for simple objects, but it can become expensive when constructors perform heavy work such as database connections, file I/O, or large collection population. This cost is paid on every instantiation, so it is often better to use a factory method or a builder that separates construction from configuration.
Another pitfall is relying on the order of static initialization across classes. If class A's static initializer references class B, and B's static initializer references A, you can end up with null values or circular loading complications. The JVM handles this by allowing the first class to see the second in an incomplete state.
For objects that are expensive to create, consider caching or pooling if the object is immutable and safe to share. But do not add complexity unless profiling shows that construction is a real bottleneck. The default object initialization mechanism is efficient for the vast majority of use cases.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.