CodeGym /Courses /JAVA 25 SELF /Object initialization: initialization order

Object initialization: initialization order

JAVA 25 SELF
Level 14, Lesson 5
Available

1. Object initialization order in Java: who’s first, who’s last?

Creating an object in Java is not just “allocate memory and write values there.” It’s a whole ritual with a clear sequence of actions. If you imagine the initialization process as a rocket launch, each stage has its strict place.

When you write:

Person user = new Person("Vasya", 30);

Java performs the following steps:

  1. Allocates memory for the object and assigns default values to all fields.
  2. Performs explicit field initializations (if you specified values right at the declaration).
  3. Executes all instance (non-static) initialization blocks — in the order they appear in the class.
  4. Executes the body of the constructor you invoked with new.

Let’s look at each step in detail.

2. Field initialization

Default values

When an object is just being created (before you explicitly assign anything), all its fields receive default values:

Field type Default value
int, short, byte, long
0
float, double
0.0
boolean
false
char
'\u0000' (null character)
Reference types (String, other objects)
null

Explicit initialization

If you assign a value to a field right at its declaration, that value will be set after default values but before the constructor runs.

public class Person {
    private String name = "Unnamed";
    private int age = 18;
}

If you create an object via the no-arg constructor, these values will remain. If you use a constructor with parameters, they will most likely be overridden.

3. Initialization blocks: why they exist and how they work

In Java you can declare so-called instance (non-static) initialization blocks. They run every time an object is created, right after field initialization but before the constructor.

Syntax:

public class Person {
    {
        System.out.println("Executing instance initialization block!");
    }
}

If a class has multiple such blocks, they are executed in the order they are written.

Example with multiple blocks

public class Person {
    private String name = "Unnamed";

    {
        System.out.println("Block 1: name = " + name);
        name = "Mystery";
    }

    {
        System.out.println("Block 2: name = " + name);
    }

    public Person() {
        System.out.println("Constructor: name = " + name);
    }
}

If you create an object:

Person p = new Person();

The output will be:

Block 1: name = Unnamed
Block 2: name = Mystery
Constructor: name = Mystery

What are initialization blocks for?

In real projects, initialization blocks are used rarely. Usually, whatever you can do in a block can be done either at the field declaration or in the constructor. But sometimes (for example, when you have multiple constructors and some initialization logic must be shared among all of them) initialization blocks can be convenient.

4. Constructor: the final chord of initialization

After all fields have obtained their values (by default or explicitly) and all initialization blocks have run, the constructor is called — the one you specified after new.

In the constructor, you usually set final field values, accept parameters, and perform other initialization that depends on the input data.

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        System.out.println("Constructor: name = " + name + ", age = " + age);
        this.name = name;
        this.age = age;
    }
}

5. Demonstration of the full initialization order

Let’s write a class that clearly shows the order of initialization.

public class Person {
    private String name = "Unnamed";
    private int age = 18;

    {
        System.out.println("Initialization block: name = " + name + ", age = " + age);
        age = 21;
    }

    public Person() {
        System.out.println("No-arg constructor: name = " + name + ", age = " + age);
    }

    public Person(String name, int age) {
        System.out.println("Constructor with parameters: name = " + name + ", age = " + age);
        this.name = name;
        this.age = age;
    }

    public void printInfo() {
        System.out.println("Person: name = " + name + ", age = " + age);
    }
}

And now in our main class (for example, Main):

public class Main {
    public static void main(String[] args) {
        System.out.println("Creating object p1:");
        Person p1 = new Person();
        p1.printInfo();

        System.out.println("\nCreating object p2:");
        Person p2 = new Person("Pete", 30);
        p2.printInfo();
    }
}

The output will be approximately:

Creating object p1:
Initialization block: name = Unnamed, age = 18
No-arg constructor: name = Unnamed, age = 21
Person: name = Unnamed, age = 21

Creating object p2:
Initialization block: name = Unnamed, age = 18
Constructor with parameters: name = Unnamed, age = 21
Person: name = Pete, age = 30

Note: in the constructor with parameters, the field values are still the old ones (those after the initialization block), and only after the constructor finishes do the fields receive their final values.

6. Initialization order diagram

Here is a small diagram (flowchart) for visualization:

flowchart TD
    A[Memory allocation, default values] --> B[Explicit field initialization]
    B --> C[Execute instance initialization blocks]
    C --> D[Execute constructor]
    D --> E[Object ready for use]

7. Features and nuances

Static fields and static blocks

In this lecture we talk only about instance (non-static) fields and blocks. Static fields and static blocks are initialized once when the class is loaded, not every time an object is created. We’ll discuss them in more detail in topics on encapsulation.

When does what get initialized?

  • Fields — each time an object is created.
  • Initialization blocks — each time an object is created.
  • Static fields and blocks — only once, when the class is loaded.

Can you access fields in initialization blocks?

Yes, you can! But it’s important to remember that if a field is declared below the block, it already exists, and if you access it before its explicit initialization, you’ll get the default value.

8. Common mistakes when initializing objects

Error #1: Expecting fields to be initialized before they are declared.
In Java, the order of field and initialization block declarations in a class matters. If you access a field in an initialization block that is declared below, it already exists, but it hasn’t been explicitly initialized yet — it will have the default value.

Error #2: Duplicating initialization in blocks and constructors.
Beginners often repeat the same logic both in a block and in a constructor. It’s better to use either a block or a constructor, or to call one constructor from another via this(...).

Error #3: Expecting static fields to be initialized every time an object is created.
Static fields and blocks behave differently — they are initialized only once when the class is loaded.

Error #4: Using uninitialized reference fields.
If you forget to explicitly initialize a reference field (for example, String name;), then it will be null until you explicitly assign a value to it in an initialization block or in a constructor.

1
Task
JAVA 25 SELF, level 14, lesson 5
Locked
Observing the Appearance of Virtual Creatures
Observing the Appearance of Virtual Creatures
1
Task
JAVA 25 SELF, level 14, lesson 5
Locked
Standardizing New Books in the Publishing House
Standardizing New Books in the Publishing House
1
Task
JAVA 25 SELF, level 14, lesson 5
Locked
Tracking the start of assembly for each car on the factory line
Tracking the start of assembly for each car on the factory line
1
Task
JAVA 25 SELF, level 14, lesson 5
Locked
A Student's Path to the Final Grade
A Student's Path to the Final Grade
1
Survey/quiz
Classes and constructors, level 14, lesson 5
Unavailable
Classes and constructors
Classes and constructors
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION