#37 Kotlin Tutorial | Companion Object | Factory Pattern says you can use a Kotlin companion object when creating a class when it is complicated.
Kotlin Primary Constructor and Initializer Blocks shows how to use a Kotlin initializer block in a class.
fun main(args: Array<String>) {
val person1 = Person("joe", 25)
}
class Person(fName: String, personAge: Int) {
val firstName: String
var age: Int
// initializer block
init {
firstName = fName.capitalize()
age = personAge
println("First Name = $firstName")
println("Age = $age")
}
}
Why would you use the companion object to do this rather than the initializer block?
Regarding this question, I have no interest in Java.