Primary & Secondary Constructors & init Block in Kotlin

The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor etc.) and the class body, surrounded by curly braces. Both the header and the body are optional; if the class has no body, curly braces can be omitted.

Before starting, let’s think of a situation where you and your friend are walking on a road and somebody calls you by your name. What will you do? You will immediately turn back and respond. This is called Constructor. Here you are the class, with your name as the class name. Whenever someone calls you, with your name, you respond immediately. So, a Constructor is something that is called just after the creation of object i.e. whenever you are called by your name you will respond and this responding is the work that the constructor does.

A constructor is a concise way to initialize class properties.

It is a special member function that is called when an object is instantiated (created). However, how they work in Kotlin is slightly different.

In Kotlin, there are two constructors:

Primary constructor – concise way to initialize a class
Secondary constructor – allows you to put additional initialization logic

So, in this blog, we will learn about constructors in Kotlin. We will cover Primary Constructors, init() block and Secondary Constructors. So, let’s get started.

See Also  When in Kotlin, Switch with Super Powers

What are Constructors?
Before moving on to constructors, let’s take an example, suppose there is a class named Person, then the properties of the class will be the name of the person, age of the person, the salary of the person, etc.

The main purpose of constructor is to initialize the properties of a class. Constructor is called when we create the object of a class. In Kotlin we have two types of constructor – primary and secondary constructor. In this guide, we will learn primary and secondary constructor with example, we will also learn about initializer blocks.

So, properties are those things which help to identify you. For example, a person is identified by his name, age or place of living. There can be more than one property of a class and all of these properties must be initialized when an object is created and in order to initialize the properties of an object, we use Constructors.

See Also  Compare values & convert values to different data types in Kotlin

The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor etc.) and the class body, surrounded by curly braces. Both the header and the body are optional; if the class has no body, curly braces can be omitted.

Before starting, let’s think of a situation where you and your friend are walking on a road and somebody calls you by your name. What will you do? You will immediately turn back and respond. This is called Constructor. Here you are the class, with your name as the class name. Whenever someone calls you, with your name, you respond immediately. So, a Constructor is something that is called just after the creation of object i.e. whenever you are called by your name you will respond and this responding is the work that the constructor does.

A constructor is a concise way to initialize class properties.

It is a special member function that is called when an object is instantiated (created). However, how they work in Kotlin is slightly different.

In Kotlin, there are two constructors:

Primary constructor – concise way to initialize a class
Secondary constructor – allows you to put additional initialization logic

So, in this blog, we will learn about constructors in Kotlin. We will cover Primary Constructors, init() block and Secondary Constructors. So, let’s get started.

See Also  When in Kotlin, Switch with Super Powers

What are Constructors?
Before moving on to constructors, let’s take an example, suppose there is a class named Person, then the properties of the class will be the name of the person, age of the person, the salary of the person, etc.

The main purpose of constructor is to initialize the properties of a class. Constructor is called when we create the object of a class. In Kotlin we have two types of constructor – primary and secondary constructor. In this guide, we will learn primary and secondary constructor with example, we will also learn about initializer blocks.

So, properties are those things which help to identify you. For example, a person is identified by his name, age or place of living. There can be more than one property of a class and all of these properties must be initialized when an object is created and in order to initialize the properties of an object, we use Constructors.

See Also  Firebase In-App Messaging Android
d93f3bce1660660e5de55f10d6df6f1a?s=96&d=mm&r=g
SyncSaShttps://syncsas.com
SyncSaS Technologies is specialized in creating and designing customized software. We have our own team that will be in charge of developing your softwares.

More from author

Related posts

Advertismentspot_img

Latest posts

How to increase YouTube Subscribers 2 Organic Strategy

How to increase YouTube Subscribers Being a YouTube personalities everyone wants the number of channel subscribers should multiply very quickly. Making YouTube video and uploading...

5 Ways to Increase Your YouTube Views

Increase Your YouTube Views Try going back and editing it to make it even better, and work on new videos. A helpful tip is to...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!