Partner – Microsoft – NPI (cat=Java)
announcement - icon

Microsoft JDConf 2024 conference is getting closer, on March 27th and 28th. Simply put, it's a free virtual event to learn about the newest developments in Java, Cloud, and AI.

Josh Long and Mark Heckler are kicking things off in the keynote, so it's definitely going to be both highly useful and quite practical.

This year’s theme is focused on developer productivity and how these technologies transform how we work, build, integrate, and modernize applications.

For the full conference agenda and speaker lineup, you can explore JDConf.com:

>> RSVP Now

1. Overview

In this quick tutorial, we’re going to focus on the return type for a constructor in Java.

First, we’ll get familiar with how object initialization works in Java and the JVM. Then, we’ll dig deeper to see how object initialization and assignment work under-the-hood.

2. Instance Initialization

Let’s start with an empty class:

public class Color {}

Here, we’re going to create an instance from this class and assign it to some variable:

Color color = new Color();

After compiling this simple Java snippet, let’s take a peek at its bytecode via the javap -c command:

0: new           #7                  // class Color
3: dup
4: invokespecial #9                  // Method Color."<init>":()V
7: astore_1

When we instantiate an object in Java, the JVM performs the following operations:

  1. First, it finds a place in its process space for the new object.
  2. Then, the JVM performs the system initialization process. In this step, it creates the object in its default state. The new opcode in the bytecode is actually responsible for this step.
  3. Finally, it initializes the object with the constructor and other initializer blocks. In this case, the invokespecial opcode calls the constructor.

As shown above, the method signature for the default constructor is:

Method Color."<init>":()V

The <init> is the name of instance initialization methods in the JVM. In this case, the <init> is a function that:

  • takes nothing as the input (empty parentheses after the method name)
  • doesn’t push any value onto the operand stack (represented by the V, typically associated with void).

In summary, while the bytecode representation for a constructor shows a return descriptor of V, it would be inaccurate to state that constructors in Java have a void return type. Instead, constructors in Java simply don’t have a return type.

So, taking another look at our simple assignment:

Color color = new Color();

Now that we know how constructors work, let’s see how the assignment works.

3. How Assignment Works

JVM is a stack-based virtual machine. Each stack consists of stack frames. Put simply, each stack frame corresponds to a method call. In fact, JVM creates frames with a new method call and destroys them as they finish their job:

simple ol

Each stack frame uses an array to store local variables and an operand stack to store partial results. Given that, let’s take another look at the bytecode:

0: new           #7                // class Color
3: dup
4: invokespecial #9               // Method Color."<init>":()V
7: astore_1

Here’s how the assignment works:

  • The new instruction creates an instance of Color and pushes its reference onto the operand stack
  • The dup opcode duplicates the last item on the operand stack
  • The invokespecial takes the duplicated reference and consumes it for initialization. After this, only the original reference remains on the operand stack
  • The astore_1 stores the original reference to index 1 of the local variables array. The prefix “a” means that the item to be stored is an object reference, and the “1” is the array index

From now on, the second item (index 1) in the local variables array is a reference to the newly created object. Therefore, we don’t lose the reference, and the assignment actually works — even when the constructor returns nothing!

4. Conclusion

In this quick tutorial, we learned how the JVM creates and initializes our class instances. Moreover, we saw how the instance initialization works under-the-hood.

For an even more detailed understanding of the JVM, it’s always a good idea to check out its specification.

Course – LS (cat=Java)

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!