https://kotlinlang.org/docs/reference/classes.html
Here there is a section that mentions
NOTE: On the JVM, if all of the parameters of the primary constructor have default values, the compiler will generate an additional parameterless constructor which will use the default values. This makes it easier to use Kotlin with libraries such as Jackson or JPA that create class instances through parameterless constructors.
class Customer(val customerName: String = “”)
I want to point out usage of ‘val’ in the primary constructor here.
If we have ‘val’ property that means it cannot be modified. So how does that ‘generated parameterless constructor’ helps here. Jackson/JPA or any code calling that parameterless constructor won’t be able to change the value after create object using it right ?
class Person( val name: String = “”, val age: Int = -1)
fun main(args: Array) {
val p1 = Person()
p1.age = 110 // compile error - Val cannot be reassigned
}