Без рубрики

Тип возврата конструктора в Java

Узнайте о типе возврата конструктора в Java

Автор оригинала: Mona Mohamadinia.

1. Обзор

В этом быстром учебнике мы сосредоточимся на типе возврата для конструктора на 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."":()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."":()V

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

  • takes nothing as the input (empty parentheses after the method name)
  • returns nothing (V stands for void )

Therefore, the return type of a constructor in Java and JVM is void.

Taking another look at our simple assignment:

Color color = new Color();

Now that we know the constructor returns void , 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:

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."":()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 .