As described in the previous article, A constructor of the class is called to create an object. If there are no specific instructions regarding a class-level constructor, the compiler generates an empty one by default Parameterless constructor, as shown in the following example:

But it is also possible to create your own constructors using the keyword constructor to define a class. One distinguishes between primary and secondary Constructors. A class can have a primary constructor and several secondary constructors. This flexibility enables differentiated initialization of objects.

Primary constructor in Kotlin

The primary constructor is part of the class header and is defined directly after the class name.

Similar to functions, constructors in Kotlin can be provided with parameters that allow To accept data from outside for object initialization. For example, in the example above, the constructor has the parameter make of the type String.

If the primary constructor has no annotations or access modifiers, the keyword constructor be omitted.

Initializer

An initializer in Kotlin is a block of code within a class that is executed when a class instance is created. These blocks are with the keyword init marked. They allow data passed through class parameters to be passed to class properties and, if necessary, to perform additional validation or setup steps. In the code below we consider the simple case – passing data to class parameters: 

The constructor takes the parameter _make of type String. Within the class there is a property make of the type String declared. In the init-Block, an initializer, this property becomes the value of the parameter _make assigned. This means that when creating an object class Car, the value for make directly through the passed constructor parameter _make is determined.

Particularly worth mentioning is that the property make has no initial value because it is always initialized in the initialization block. When an object is created, this property is always given a value.

Now we use our class to create multiple objects: 

It is important to note that if a primary constructor is defined in a class, it will be used to create an object. It is not possible to use a compiler-generated default constructor and a primary constructor at the same time.

It should also be noted that in this case an initialization block is not necessary since the parameters of the primary constructor can be assigned directly to the properties. This leads to a significant simplification of our code, as can be seen in the following example:

Primary constructor and properties

The primary constructor can also be used to define properties (attributes). Properties can be created directly in the constructor using val (for immutable properties) or var (for variable properties). This simplifies and shortens the code structure because properties do not have to be defined separately in the class body, but are assigned directly in the constructor. This makes the code clearer and more efficient.

Secondary constructor

Secondary constructors in Kotlin are used to provide additional and more flexible ways to create objects of a class. They allow different construction logic to be defined within the same class, which can differ in the number and type of parameters. This is particularly useful when you want to create objects under different conditions or with different initialization values.

Secondary constructors are defined within the class. If the class has a primary constructor set, the secondary constructor calls the primary with the keyword this .

The primary constructor takes the parameter _make opposite, which is used to initialize the immutable property make is used.

It also becomes a mutable property engineSize initialized with a default value of 0.00f.

The secondary constructor takes two parameters, _make and _engineSize, in contrast to. It calls the primary constructor this(_make) and then sets the value of engineSize on the value of _engineSize.

Now we use the created Car class and initialize two different objects:

At the nuclear hand-Function will be two objects of the class Car created. For creating the object car1 the primary constructor is used, which takes one parameter. For creating the object car2 the secondary constructor with two parameters is applied.

If necessary, any number of secondary constructors can be defined:

In the class Car became a new property fuelType added which describes the fuel type of the vehicle. Another constructor has also been added that takes three parameters. To get the code for assigning the properties make and engineSize To avoid duplication, this secondary constructor passes the setting of these properties to another secondary constructor that takes two parameters by calling this(_make, _engineSize). This means this call calls the first secondary constructor with two parameters.

Result:

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked