January 2017
Intermediate to advanced
420 pages
9h 25m
English
At first, lateinit var and by lazy {...} sound quite similar. However, there are significant differences between the two of them:
lazy {...} delegate can only be used for val properties; lateinit can only be used for var properties.lateinit var property can't be compiled into a final field, hence you can't achieve immutability.lateinit var property has a backing field to store the value, whereas lazy {...} creates a delegate object that acts as a container for the value once created and provides a getter for the property. If you need the backing field to be present in the class, you will have to use lateinit.lateinit property cannot be used for nullable properties or Java primitive types. This is a restriction imposed ...Read now
Unlock full access