Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

There is uncertainty among Java developers on whether to initialize variables when they are declared or in a constructor.

In this tutorial, we’ll take a look at what happens when we initialize variables at their declaration or in a constructor. We’ll try to point out some differences and similarities between them if they exist.

2. Field Declaration Initialization

We need to know that the Java compiler takes out all field declaration initializations and moves them as code in the constructor in the order of their appearance in the class. From this, we can deduce there are no big differences between initializing variables at the declaration or in the constructor because, after compilation, they end up in the constructor anyways:

public class A {
    private B b = new B();
}

3. Constructor Field Initialization

From our above code, after compilation, we end up with this constructor:

public class A {
    private B b;

    public A() {
        this.b = new B();
    }
}

Then does it matter where we initialize data?

After knowing what happens after compilation, not that much. It’s more of a preference that we tend to form based on our experiences.

4. Guidelines

We can look into some guidelines when deciding whether to initialize data at field declaration or in the constructor.

This is a dependency injection constructor:

public A(B b) {
    this.b = b;
}

A recommended way of initializing variables is to use dependency injection. By using this way of initializing objects, we adhere to different principles.

4.1. Readability

For us to have better readability, it could be important to initialize the data either at field declaration or in the constructor, not both. Of course, we should take this as a guideline, not as a rule, since you can have both.

Having all the initialization in one place makes it easier to know where to look for the initialization code. This will make it easier for any future developer to look at initialization in only one place, not scattered around multiple places.

4.2. Single Responsibility

By using the dependency injection constructor, we remove the responsibility of instantiating the B object from the A class. Following the single responsibility principle is always preferable where possible.

This will also mean we will have lower coupling between classes, another awesome guideline to follow.

4.3. Testability

Using this constructor tends to have easier ways to test our code since we can easily mock our B object and then inject it into our A object with this constructor:

B b = mock(B.class);
A a = new A(b);

4.4. Maintainability

Having lower coupling classes also brings us better maintainability of the code. Thus, we’ll be able to modify the code easier.

5. Conclusion

In this article, we’ve seen that using either field declaration or constructor initialization is more of a preference, and some differences between them can be the design principles that we want to follow.

Using the constructor initialization and dependency injection can give us some advantages of design principles.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW