Читать книгу Programming Kotlin Applications - Бретт Мак-Лахлин, Brett McLaughlin - Страница 81
Every Property Must Be Initialized
ОглавлениеThis doesn't look a lot different. However, there's a problem. Compile this code and you're going to get an error that, by now, is probably becoming a bit familiar:
Error:(5, 5) Kotlin: Property must be initialized or be abstract
What's going on here? Well, it's a little bit of a pain. Remember, any property, must either be assigned an initial value when they are declared (like firstName
and lastName
, for example), or be assigned a value in the init
block.
Now, it looks like that's what is happening, but Kotlin doesn't quite follow your code the same way that you do. While it is possible to see that when init
calls updateName()
, then fullName
will get a value, Kotlin just sees that there's no assignment to fullName
in init
and throws an error.