Explain what is wrong with that code?

Technology CommunityCategory: KotlinExplain what is wrong with that code?
VietMX Staff asked 3 years ago
Problem

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) {
    }
}

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
    }
}