0 votes
in Kotlin by
Explain what is wrong with that code?

Why is this code wrong?

class Student (var name: String) {

    init() {

        println("Student has got a name as $name")

    }

    constructor(sectionName: String, var id: Int) this(sectionName) {

    }

}

1 Answer

0 votes
by
The property of the class can’t be declared inside the secondary constructor.. This will give an error because here we are declaring a property id of the class in the secondary constructor, which is not allowed.

If you want to use some property inside the secondary constructor, then declare the property inside the class and use it in the secondary constructor:

class Student (var name: String) {

    var id: Int = -1

    init() {

        println("Student has got a name as $name")

    }

    

    constructor(secname: String, id: Int) this(secname) {

        this.id = id

    }

}
...