4

I have tried to set a property value as in the following snippet.This SO question is not answering the question.

var person = Person("john", 24) //sample_text.text = person.getName() + person.getAge() var kon = person.someProperty person.someProperty = "crap" //this doesn't allow me to set value kon = "manulilated" //this allows me to set the value sample_text.text = kon class Person(val n: String, val a: Int){ var pname: String = n var page: Int = a var someProperty: String = "defaultValue" get() = field.capitalize() private set(value){field = value} fun Init(nm: String, ag: Int){ pname = nm page = ag } fun getAge(): Int{ return page } fun getName(): String{ return pname } } 

Why was I able to set the value of the Person class on the second line but not the first line?

1
  • 2
    Btw there is absolutely no point for providing the getAge() and getName() functions - kotlin automatically creates getters for all variables (apart from those annotated with @JvmField) Commented Dec 29, 2017 at 5:58

3 Answers 3

7

First, the private modifier is your problem.

Change

private set(value){field = value} 

To

set(value){field = value} //public by default 

Otherwise you can’t use the setter outside the class. Read here.

For members declared inside a class: private means visible inside this class only (including all its members);

Second, you’re misunderstanding something:

 var kon = person.someProperty kon = "manulilated" 

In these lines you’re not changing the property in the object. After creating the variable kon, being a String pointing to someProperty, you reassign that local variable to something else. This reassignment is unequal to changing the value of person.someProperty! It has totally no effect in the object.

Sign up to request clarification or add additional context in comments.

Comments

3

someProperty has a private setter. You can't set it outside the class when the setter is private

Comments

0

This is a good example of trying to write Kotlin in Java style.

Here is how this would like in Kotlin facilitating the language capabilities. One can see how much shorter and clearer the code is. The Person class has only 3 lines.

Using data class spares us from defining hash and equals and toString.

fun test() { val person = Person("john", 24) var kon = person.someProperty person.someProperty = "crap" //this doesn't allow me to set value kon = "manulilated" //this allows me to set the value (yeah, but not in the class, this is a local string) val sample_text = SampleText("${person.name}${person.age}") sample_text.text = kon println("Person = $person") println("Kon = $kon") println("Sample Text = $sample_text") } /** class for person with immutable name and age * The original code only has a getter which allows to use 'val'. * This can be set in the constructor, so no need for init code. */ data class Person(val name: String, val age: Int) { /** The field value is converted to uppercase() (capitalize is deprecated) * when the value comes in, so the get operation is much faster which is * assumed to happen more often. */ var someProperty: String = "defaultValue" // setter not private as called from outside class set(value) { field = value.uppercase() } } /** simple class storing a mutable text for whatever reason * @param text mutable text */ data class SampleText(var text: String) { override fun toString(): String { return text } } 

This is the result:

Person = Person(name=john, age=24) Kon = manulilated Sample Text = manulilated 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.