Friday, January 30, 2026

Constructor

JVM Object Creation Process

How the JVM Creates a New Object

When we ask the JVM to create a new instance of a class, the JVM performs four steps:

First, it checks whether that class has been initialized or not. If not, the JVM loads and initializes the class first.

Second, it allocates the memory required to hold the instance variables of the class in the heap space.

Third, it initializes these instance variables to their default values (i.e., numeric and char variables to zero, boolean variables to false, and reference variables to null).

Finally, the JVM gives that instance an opportunity to set the values of the instance variables as per the business logic of that class by executing code written in special sections of that class. These special sections are: instance initializers and constructors.

It is only after these four steps are complete that the instance is considered “ready to use”. Remember the use of the new operator to create instances of a class? The JVM performs all four activities mentioned above and only then returns the reference of the newly created and initialized object to your code.

Out of the four activities listed above, you have already seen the details of the first one in the previous section. The second two activities are performed transparently by the JVM.

Constructor of a class ☝ A constructor of a class looks very much like a method of a class but there are two things that make a method a constructor: Name- The name of a constructor is always exactly the same as the name of the class. Return type- A constructor does not have a return type. It cannot even say that it returns void.

No comments:

Post a Comment

CompletableFuture

  Welcome back to  our concurrency series ! In our first discussion, we likely touched on the traditional models of threading. Today, we’re ...