Ellen Spertus
01/07/2023, 8:46 PMIn Kotlin, a property is a field that has additional logic associated with it, such as a getter and a setter. A property can be either a(read-only) or aval
(read-write).var
An instance variable, on the other hand, is a field that is declared directly within a class. It does not have any additional logic associated with it and can be accessed directly from instances of the class.Does Kotlin have instance variables (distinct from properties)?
Chris Lee
01/07/2023, 9:11 PMGleb Minaev
01/07/2023, 9:12 PM@JvmField
. It will be translated to good old field.
For example,
abstract class A {
val var1 = 5 // this is a property variable with a backing field
abstract val var2: Int // this is a property variable without a backing field
lateinit var var3: String // this is a property variable with a backing field as well
fun action() {
val var3 = var1 * (var1 + 1) // this is an instance variable
val var4: Int // this is an instance variable as well
val var5 by lazy { 15 } // this is a property variable
}
}
val A.var6 get() = "Hello, var5" // this is a property variable too
Chris Lee
01/07/2023, 9:14 PMEllen Spertus
01/07/2023, 9:30 PMclass Address {
var name: String = "Holmes, Sherlock"
var street: String = "Baker"
var city: String = "London"
var state: String? = null
var zip: String = "123456"
}
https://kotlinlang.org/docs/properties.htmlEllen Spertus
01/07/2023, 9:31 PMCLOVIS
01/07/2023, 9:31 PMEllen Spertus
01/07/2023, 9:32 PMclass ArrayList<T> : List<T> {
private var array: Array<Any?> = arrayOfNulls(10)
private var size = 0
override val size: Int
get() = size
it defended its reuse of the name size
but the compiler said otherwise.Chris Lee
01/07/2023, 9:32 PMJosh Eldridge
01/07/2023, 9:32 PMChris Lee
01/07/2023, 9:33 PMEllen Spertus
01/07/2023, 9:34 PMEllen Spertus
01/07/2023, 9:34 PMEllen Spertus
01/07/2023, 9:35 PMJosh Eldridge
01/07/2023, 9:35 PMChris Lee
01/07/2023, 9:36 PMChris Lee
01/07/2023, 9:36 PMCLOVIS
01/07/2023, 9:37 PMlazy
) but can't have custom getters or settersEllen Spertus
01/07/2023, 9:37 PMJosh Eldridge
01/07/2023, 9:38 PMChris Lee
01/07/2023, 9:39 PMCLOVIS
01/07/2023, 9:40 PMconst
.Chris Lee
01/07/2023, 9:42 PMChris Lee
01/07/2023, 9:43 PMall top-level “variables” are properties except when marked withWell, almost - you can’t have custom getter/setter on variables. You can do delegation and control mutability.const
Josh Eldridge
01/07/2023, 9:44 PMChris Lee
01/07/2023, 9:47 PMJosh Eldridge
01/07/2023, 9:48 PMJosh Eldridge
01/07/2023, 9:50 PMChris Lee
01/07/2023, 9:51 PMChris Lee
01/07/2023, 9:58 PMpropertyDelcaration
is both top-level and class-level, from a grammar perspective, though from a semantics perspective there are differences.Josh Eldridge
01/07/2023, 10:01 PMthough from a semantics perspective there are differences.Are those the differences that was discussed above about
const
and things like delegation?Chris Lee
01/07/2023, 10:01 PMJosh Eldridge
01/07/2023, 10:01 PMChris Lee
01/07/2023, 10:02 PMJosh Eldridge
01/07/2023, 10:04 PMChris Lee
01/07/2023, 10:05 PMpublic class Foo {
public val bar : String
get() = "whatever"
public val xyz: String = "abc"
public var zzz : String = "Abc"
public val lazyThing : Lazy<String> = lazy { "abc" }
public companion object {
public const val something: String = "someconst"
}
}
public val bar : String
get() = "abc"
public val xyz: String = "abc"
public var zzz : String = "Abc"
public const val something : String = "whatever"
public val lazyThing : Lazy<String> = lazy { "abc" }
// can't do this directly in a class, only in a companion object
public const val sss: String = "abc"
Chris Lee
01/07/2023, 10:05 PMChris Lee
01/07/2023, 10:07 PMpublic fun something() {
// no-go - doesn't compile
// val bar : String
// get() = "abc"
val xyz: String = "abc"
var zzz : String = "Abc"
// no-go - doesn't compile
//const val something : String = "whatever"
val lazyThing : Lazy<String> = lazy { "abc" }
}
Chris Lee
01/07/2023, 10:08 PMpropertyDeclaration
according to the grammar but are scoped down in their capabilities.Josh Eldridge
01/07/2023, 10:24 PMKirill Grouchnikov
01/08/2023, 3:33 AMefemoney
01/08/2023, 8:57 AM