Java Tips — Creation of a new object
By: Marco Domenico Marino
Tags: Programming Tips, Java, Programming, Programming Languages

Java Tips — Creation of a new object

Different ways to declare and initialize a new object in Java

Photo by Samuel Zeller on Unsplash

To use an object in Java is essential to define and, when need to use, to execute the command of new provided by the syntax.

With the command of new is invoked the Java Virtual Machine that allocates the position in memory and gets the pointer associated with it after is called the specified constructor that initializes the memory area: now the object is usable.

There are several ways to declare and make usable an object and now try to describe them.

Constructor method in a class object

After start to describe the ways to initialize it is useful to recall the necessary step to create an instance of an object.

Every object is described into a Java class file: the file needs to have the same name as the object and also must be the same asthe class name. To initialize the object at last is necessary defines one or more constructor method that permits the initialization.

This is the definition get from the Oracle Java documentation:

Declaration: The code set in bold are all variable declarations that associate a variable name with an object type. Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

This is an example of default constructor and constructor with parameters:

Constructor examples

With the first type of constructor the operation of new initialize the attributes with their default values after the instantiation of the object is completed; the second constructor method, unlike the first, allows to the caller to provide in input a set of values only itself use to initialize the local attributes of the object.

Initialization in-line

The most common use for creating objects is when-is-needed.

Often the objects are defined, instantiated and initiated in a single line: this definition way expects the use of a type and a variable name. The type must be the same as the object that it would create.

After the declaration is immediately invoked the constructor method:

Declaration and initialization in-line of object

With the initialization in-line the object instantiated are visible only within the method or portion of the method enclosed in braces { }. These objects are not usable outside the braces.

The initialization in-line is used when a developer needs to have an instance of an object and this instance needs to be accessible only into a specific portion of the class.

Separate declaration and initialization

In any case, the declaration and initialization of the object occur at different times.

The declaration of the objects is positioned in a line and the instantiation and the initialization is in a different line. With this approach the declaration drives the visibility of the objects into the execution of the program.

Separate declaration and initialization of objects

If the declaration is defined as a class variable, the object, once instantiated, is visible in every portion of the class execution; otherwise apply the rules of the in-line declaration.

Initialization by reflection

An other opportunity to execute the initialization of the objects is the reflection; using the reflection is it possible to identify the right constructor method from a class definition and invoke it using the reflection library included in the Java SE.

The initialization by reflection is applicable in in-line mode or in separate mode.

Initialization in-line of object by reflection

This approach is useful in context when is required an high degree of dynamism for example when developing a library or a framework: in these cases it is difficult (and not performing) defines all objects that you need to you use, so with the initialization by reflection it is possible to predispose the software to work with generic information.

Conclusion

We have retraced some ways to declare, initialize and instantiate the object in Java: from the classical initialization in-line until the use of the reflection.

For the topic of an object creation topic was taken origin a lot of design patterns (ref. definition) to make reusable solution to create objects; in particular there is a category of design patterns in which are contained the Creational Pattern.

Git: repository