PK - Chapter 6 Flashcards
What is a backing field?
Generated for a property in a class if it uses the default implementation of at least one of the accessors.
How do you access a backing field?
The backing field can be accessed
inside a custom accessor using the field keyword.
var firstName : String
get() = field
set(value) {field = value}
Why does a Property
need a Backing Field?
object.SomeProperty = “value”
is the same as
object.setSomeProperty(“value”)
so the following would be recursive
and generate a StackOverflow
———————-
var firstName : String
get() = firstName
set(value) {firstName = value}
How would you create a public property
with a private setter?
var firstName : String
get() = firstName
set(value) {firstName = value}
What is the purpose of lateinit?
Non-null properties have to be
initialized in the constructor, unless
they are defined with the lateinit keyword.
lateinit var delayedInitProperty : DelayedInstance
What are the restrictions for
using Late Initialization?
The property:
- cannot be a primitive type
- cannot have custom getters or setters
What happes if you access a lateinit
property before it has been initialized ?
kotlin.UninitializedPropertyAccessException
Why use Late Initialization?
You do not initialize a non-null
variable in the constructor:
- dependency injection
- value not available during object construction
Why use a delegated property?
- to write the getters and setters only once (DRY) for a common type of property in the application domain
- to guarantee that data constraints are applied uniformly across the application/library
var eventTimestamp : Long by
TimestampPropertyDelegate()
Write the code for TimestampPropertyDelegate.
class TimestampPropertyDelegate {
private var timestamp = 0L
operator fun getValue(
ref: Any?, // object reference
property: KProperty<*>): Long {
return timestamp;
}
operator fun setValue(
ref: Any?, // object reference
property: KProperty<*>,
value: Long) {
timestamp = value
}
}
How are property delegates
similar to database domain constraints?
You can create a data dictionary of
property delegates that enforce
data integrity for the logical data types
in your application domain.
A change to a property delegate in the
data dictionary will consistently
propogate across the application.
What interface(s) should a property delegate implement ?
- ReadOnlyProperty
- ReadWriteProperty
Why use lazy initialization?
To improve performance or resource utilization:
- you don’t want to incur the cost of initialization
unless the object is actually needed - you may want to delay initialization until more
critical processing is complete - you may want to delay initialization until
adequate resources are available
Create a lazy initialization property.
val foo: Int by lazy {
getSomeInteger()
}
What are the
LazyThreadSafeteyMode enums?
- SYNCHRONIZED
uses locks - PUBLICATION
initializer can be called several times,
but only the first value will be used - NONE
no locks are used.
high performance when it can only
be initialized by one thread