What is the use of the default constructor

Constructors

An object is created by calling a constructor. In the following example, the variable saves an object of the type after the expression has been executed.

ConstructorClass const = new ConstructorClass ();

The round brackets make it easy to see that this is a special method call that differs from other method calls by the keyword and the capitalized identifier. The reason for this is that it must be identical to the name of the class in which it was declared and whose objects it forms. Outside of a class, a constructor is never called directly, but only with a prefix. A constructor can be addressed with the keyword within your own class (see below).

As with method overloading, a class can have zero, one or more different constructors. In addition to creating the object itself, they serve to put it into a defined initial state. A constructor can be declared with or without a parameter list.
Will only a If an empty (so-called standard) constructor is required without passing parameters, it does not have to be specified explicitly. If it is missing, it is generated automatically by the compiler. However, this is only the case as long as no further constructor of the class has been declared. Conversely, if an empty standard constructor is required in addition to other constructors, this must be specified explicitly.

public class constructorClass {
    
private int x;

// default constructor
public constructorClass () {
    }
    
// second constructor
public constructorClass (int i) {
this.x = i;
    }
}

The example shows a class with two constructors. The first is a standard constructor that leaves the variable uninitialized when the object is created. The second also causes the initialization of these instance variables. If an object of the class is to be created, this can be done with or without specifying an initial value for the instance variable. It can thus be done as follows:

ConstructorClass const = new ConstructorClass ();

or:

ConstructorClass const = new ConstructorClass (2);

Constructors must follow some rules:

  • They always have the name of the class.
  • They can be overloaded (multiple constructors, see above).
  • They have no return value.
  • If you do not call any other constructor on your part, the compiler will add the call to the constructor of the base class.

Since they must be accessible from the outside, one might assume that constructors are always declared. This is usually the case. However, there are exceptions. An example of this is the constructor of a singleton that is declared and called via a static method. See the article under → Design Patterns / Singleton Pattern.

Mutual constructor call

Constructors can also call each other reciprocally within a class. This is widely used in the Java standard library. A section of the class is given as an example, the objects of which represent a rectangle of a defined size at a certain position. These values ​​must therefore be specified when the object is created.

// ... int x, y, width, height; // ... public rectangle (int x, int y, int width, int height) {this.x = x; this.y = y; this.width = width; this.height = height; } // ... public Rectangle (int width, int height) {this (0, 0, width, height); }

In the first constructor, the parameters contain the values ​​for the position and those for the size of the rectangle. The parameter values ​​are transferred to the corresponding instance variables within the constructor.
You can see the principle here: In the second constructor, the first is called via the keyword (the unambiguous parameter list ensures that the correct constructor is addressed). The first two of its four parameters, the position of the rectangle, are assigned the constant values; the transferred parameters for width and height are passed on directly to the last two. The actual object formation is done by the first constructor, which is referenced by the second. When using the second constructor, a rectangle is created at a standard position; when using the first, this can be specified as required.