Besides the other answers, there's another way. Initialize the properties directly using the primary constructor parameters:
class Student(id: String, name: String) { var id: String? = id var name: String? = name var grade: String? = null constructor(id: String, name: String, grade: String) : this(id,name) { this.grade = grade } }
Note, since id and name are always initialized with a non-nullable value, you can omit the ?. Apart from that, you can omit the secondary constructor using a default value in the primary constructor:
class Student(id: String, name: String, grade: String? = null) { var id: String = id var name: String = name var grade: String? = grade }
But now we only have one constructor left, so we can pull the properties directly into the constructor:
class Student( var id: String, var name: String, var grade: String? = null )
Because the body of the class is now empty, I also omitted the curly braces.