Let's look at the mentioned steps to initialize a constructor:
- Kotlin provides a syntax that can initialize the properties with much less code. Here's what class initialization looks like in Kotlin:
class Student(var roll_number:Int, var name:String)
- You don't even need to define the body of the class, and the initialization of properties takes place in the primary constructor only (the primary constructor is part of the class header). Obviously, you can either choose var or val, based on whether you need to keep your properties mutable or not. Now, if you try to create an object, you can do so with this:
var student_A=Student(1,"Rashi Karanpuria")
- Just to confirm, let's try to print its properties to see whether we were ...