Initialization
Kotlin Study Group
John
Hi, I am John
- Android Developer
- Work on Rakuten Viki
- Use Kotlin for 2 years
- Linkedin:
https://www.linkedin.com/in/chang-john-0a95237a/
Outline
- Preface
- Constructors
- Primary Constructor
- Secondary Constructor
- Initializer block
- Property Initialization
- Initialization Order
- Delaying mechanism for initialization
- Delaying Initialization vs Nullable Type
- Lazy Initialization
- Initialization Gotchas
- Challenge: The Riddle of Excalibur
Preface
- When you read, think what’s different syntax between Java and Kotlin.
- Kotlin is null safety rule.
- Kotlin is idiomatic language, meaning less boilerplate.
- The class in Kotlin contains class property, not only field(data)
- Property = field + accessor(get/set)
- Class Property must give initial value, except computed property
Constructors - similar with Java Constructor Syntax
Kotlin Primary Constructor - more than you think
Second Constructor - alternative constructed way
Default arguments and Named arguments support
Initializer block
Property Initialization
Initialization Order
- Primary Constructor
- Secondary Constructor
- Init Block
- Property Initialization
Initialization Order
Initialization Order
1. Primary constructor
2. Property initialization/Init order depend on who write first
3. Secondary constructor
Delaying Initialization
Nullable Property
Lazy initialization
What's mystery of lazy delegation?
Initialization Gotchas
1. Refer to property before at init block
2. Refer to property before at property initialization
3. Complexity assignment in the initialization
Challenge: The Riddle of Excalibur
Reference
- When to Use Lazy or Lateinit
- Programmer Dictionary: Delegation vs Composition
- Simple one-liner ViewBinding in Fragments and Activities with Kotlin

Initialization