While being a Java developer, we usually create lots of objects daily, but we always use new or dependency management systems, e.g., Spring, to create these objects. However, there are more ways to create objects which we are going to study in this article.
There are five core ways to create objects in Java, which are explained below with their example, followed by the bytecode of the line creating the object. However, lots of Apis are out there which create objects for us, but these APIs will also are using one of these five core ways indirectly, e.g., Spring BeanFactory.
If you execute the program given in the end, you will see that method 1, 2, and 3 uses the constructor to create the object, while 4 and 5 don’t call the constructor to create the object.

1. Using New Keywords

It is the most common and regular way to create an object and a very simple one also. Using this method, we can call whichever constructor we want to call (no-arg constructor and parameterized).

Leave a Reply

Your email address will not be published. Required fields are marked *