Q: Val cannot be reassigned error in constructor I want to reassign a new value in the constructor, but I can't.
class InstantDate {

    private var date: LocalDateTime? = null

        constructor(date: String) {
        if (date.contains(" ")) 
        {
            date = date.replace(" ", "T")
            this.date = LocalDateTime.parse(date)
        }
    
    }

}

I get the error "val cannot be reassigned."
How can I solve this without creating a new value? Thank you.

A: Methods and constructor parameters are implicitly vals in Kotlin and cannot be re-assigned. Either create local var, or maybe inline your replace operation on string like so:
class InstantDate {
    private var date: LocalDateTime? = null
    
    constructor(date: String) {
        if (date.contains(" ")) {
            this.date = LocalDateTime.parse(date.replace(" ", "T"))
        }
    }
}

