Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Partner – Frontegg – expanded (cat = Security)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Partner – Frontegg – expanded (cat = Spring Security)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

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.

As always, the source code for the examples is available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!