https://kotlinlang.org logo
#getting-started
Title
# getting-started
b

blue

07/27/2020, 2:56 PM
Hi everyone, I am new to Kotlin, I have been learning Kotlin thru hyperskill.org for past few weeks. Currently I am little confused with a small Quotation in Hyperskill study material, Can someone help me? In Kotlin do we need to initialize the class level mutable ( 
var
 ) property during declaration? *T*he study material says -
full syntax for any property in Kotlin:
Copy code
var <propertyName>[: <PropertyType>] [= <property_initializer>]
    [<getter>]
    [<setter>]
Initializer, getter, and setter are optional here, but you need to initialize your variable if you don’t want to specify its type. Otherwise, the compiler will warn you.
But my code snippet is throwing compile error. Did I miss something?
Copy code
class User {
    var firstName: String 
    var lastName: String
}
j

João Gonçalves

07/27/2020, 2:58 PM
firstName
and
lastName
are of type String which means that they aren’t optional. For this reason, you either initialize the values or provide a constructor to initialize them
t

thanksforallthefish

07/27/2020, 3:00 PM
if by optional you mean nullable that is not the reason. but it looks like hyperskill is wrong, as far as I know if you don’t declare the property as
lateinit
you need an initial value. kotlin wants you to be explicit, basically you are assuming the responsibility of initializing the value before using the variable
🙂 1
b

blue

07/27/2020, 3:01 PM
https://hyperskill.org/learn/step/7362 I am referring to study material here
Initializer, getter, and setter are optional here, but you need to initialize your variable if you don’t want to specify its type.
t

thanksforallthefish

07/27/2020, 3:01 PM
so, either
Copy code
class User(var firstName: String, var lastName: String)
or
Copy code
class User {
  lateinit var firstName: String
  lateinit var lastName: String
}
or
Copy code
class User {
  var firstName = ""
  var lastName = ""
}
b

brandonmcansh

07/27/2020, 3:04 PM
any reason why User can't be a data class?
data class User(val firstName: String, val lastName: String)
p

pedro

07/27/2020, 3:05 PM
Copy code
class Example(
    private var property: String
) {
    init {
        property = "hello"
    }
}
This is valid kotlin and the value is not initialised at the property declaration. So this might be nitpicking a bit but what they said is correct
t

thanksforallthefish

07/27/2020, 3:10 PM
well, the block is called
init
it is technically run during object initialization, so the property is initialized when the object is constructed. also,
Copy code
class Example(
    private var property: String
)
is valid, because you need to pass a value when instantiating an
Example
they refer to the syntax
Copy code
class Example {
  var property: String
}
but I don’t the sentence, initializer is not optional. ofc you can write
Copy code
class Example {
  var property: String
  init { property = "" }
}
but a). nowhere in that page they mention the construct and b).
init
is run at object initialization, so the initializer is there
p

pedro

07/27/2020, 3:34 PM
Yes, you corrected my example from a constructor parameter to a property of the class (not sure these terms are technically correct) 👍 The question said “*during declaration*” so when you argue
block is called 
init
 it is technically run during object initialization, so the property is initialized when the object is constructed.
What you said is correct but the original question is not talking about initialisation. Just declaration. I did say my answer was nitpicking 🙂 I still think this might be what they were thinking about when they wrote it.
t

thanksforallthefish

07/28/2020, 6:30 AM
@pedro in my hobby (board gaming) you would be called rule lawyer 😛 but fair point, you warned me and you might be right. I skimmed through the comments on the resource linked and it seems other people are confused as well but never got an official answer. and it seems there are others mistakes/inconsistencies as well though
😅 1
4 Views