1

in the below code I am trying to create a getter method as a backing field. so that, when the getLastNameLen property is invoked it should return the length of the lastNameset.

please refer to the code below and help me to fix the bug.

how to display output of the backing fields

code:

class Thomas (val nickname: String?, val age : Int?) {

    //backing field 1
    var lastName : String? = null
        set(value) {
            if (value?.length == 0) throw IllegalArgumentException("negative values are not allowed")
            field = value
            println("lastname backing field set: ${field} ")
        }

    val getLastNameLen
        get() = {
            this.lastName?.length
        }
}

output

lastname backing field set: jr.stephan 
lastName is jr.stephan
lastNameLen is () -> kotlin.Int?
4

1 に答える 1