sterger.blogg.se

Kotlin type any
Kotlin type any













kotlin type any

The syntax of this block contains the init keyword and the code block.Īs you can see here, name and roll are accepting the values Ram and 7. So you use the init block to put the initialization code. The primary constructor in Kotlin cannot contain any code, but you have to put the initialization logic code somewhere. Primary Constructor With Initializer Block Here, S_record is the object of the Record class, and when it is created, it initializes the name and roll with values Ram and 7, respectively. This primary constructor has two properties: name and roll. Here, (val name: String, var roll: Int), is the primary Constructor. In Kotlin language, the Constructor is declared with the class header, unlike Java, where you have to declare the Constructor in the class body.

kotlin type any

There are two types of Constructors in Kotlin.

kotlin type any

Now, have a look at types of constructors. To initialize those properties, you need an object, and the Constructor is used to initialize the object with a particular initial value. The class has many properties, which must be initialized. Now, understand the need for a constructor and the types of constructors. The working of the constructor in Kotlin is a bit different, which you will look into later.

kotlin type any

Similarly, whenever you create an object of the class, the constructor is called automatically before it makes the function calls. So this is the protocol that is done automatically whenever a customer orders something. When the class object is created, it automatically calls the constructors before any function call.įor example, whenever you place an order at a restaurant, it is a general protocol that they will provide you with the cutlery and dishes before the meals are offered. A constructor can be defined as a type of member function or a special function that is used to initialize the properties of the class.















Kotlin type any