Getting error while dealing with getter and setter in kotlin

混江龙づ霸主 提交于 2019-12-02 08:44:24

You call the setter inside of the setter.. a.k.a. infinite loop:

    set(value) {
        /* execute setter logic */
        chatManger = value
    }

Inside a property getter or setter there is an additional variable available: field. This represents the java backing field of that property.

    get() = field
    set(value) {
        field = value
    }

With a regular var property, these getters and setters are auto-generated. So, this is default behaviour and you don't have to override getter / setter if all you do is set the value to a field.

It is important to remember that referring to chatManger ANYWHERE in the code ends up calling getChatManger() or setChatManger(), including inside of the getter or setter itself. This means your code will end up in an infinite loop and cause a StackOverflowError.

Read up on Properties, specifically the section about getters/setters as well as the "backing field".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!