Is a private property a "field"?

Having looked at properties in Kotlin, this concept is somewhat new for me, so I'm curious if it is right to say that the private propertyis field(instance variable)?

+4
source share
2 answers

You can consider properties as values ​​that you can get (and set for mutable), but they can have custom and excessive behavior and may not actually be saved. Therefore, properties are not fields.

In Kotlin, all properties of an element, privateor not, can have a background field , but this is not required.

  • :

    var counter = 0 
    

    0 , : , , , . , . Java getter setter.

    , , , -, , .

    var counter = 0
        get() = field + 1
        set(value) { if (value >= 0) field = value }
    

    , , get() set(...) , counter = something val x = counter. , , private .

    Backing accessors field, . , . Java-, @JvmField ( Kotlin).

  • get(), :

    val time: Long get() = System.currentTimeMillis()
    

    , backing, .


, :

val String.isCapitalized: Boolean get() = length > 0 && this[0].isUpperCase()     

private, .

+9

, " Kotlin " ( - Kotlin).

, " Kotlin Java, @JvmField" (Calling Kotlin Java - Kotlin). .

Kotlin , .

+1

All Articles