vlatkozelka
vlatkozelka

Reputation: 999

What's the right way to construct a class in Kotlin

I have a class with a lot of non null fields,and nullable ones. If I declare them like

val key : String

the Kotlin compiler will of course give me an error telling me they need to be initialized, so I figured I should put the not nullable ones in a constructor, and add a secondary constructor for when I need the nullable fields initialized with non null values. This is to avoid calling a constructor and then setters, I'd then call one constructor instead. This is what it came out looking like

import utilities.Constants
import java.util.*

class Action(val key: String,
        //val actionNode: ActionNode
             val date: Date,
             val stringValue: String,
             val autoRecognitionStatus: Constants.MeasurementAutoRecognized,
             val recognitionId: String,
             val method: Constants.MeasurementMethod,
             val userKey: String,
             val userName: String,
             val isInRange: Boolean) {


    var pictureUrl: String? = null
    var remotePictureUrl: String? = null



    constructor(key: String,
            // actionNode: ActionNode
                date: Date,
                stringValue: String,
                autoRecognitionStatus: Constants.MeasurementAutoRecognized,
                recognitionId: String,
                method: Constants.MeasurementMethod,
                userKey: String,
                userName: String,
                isInRange: Boolean,
                pictureUrl: String?,
                remotePictureUrl: String?)
            : this(key,
            // actionNode
            date,
            stringValue,
            autoRecognitionStatus,
            recognitionId,
            method,
            userKey,
            userName,
            isInRange) {
        this.pictureUrl = pictureUrl
        this.remotePictureUrl = remotePictureUrl

    }

}

My problem is that this code just looks horrible. I'm not saying that the looks is what matters the most, but I have a feeling this a wrong way to do it cause of that. Especially cause I'm having to re-write a ton of parameters when I need to delegate to the primary constructor

Is there a better, or "right" way to do this?

EDIT: What I'm asking: Did I write a lot of code for something that can be done a lot easier with Kotlin?

Upvotes: 0

Views: 396

Answers (1)

tynn
tynn

Reputation: 39843

If the class is complete like this, you can make it a data class as well. Also Kotlin supports default values for properties, so you only need one constructor, the primary one. The following class looks nicer and is almost identical to what you defined.

data class Action(
     val key: String,
     val date: Date,
     val stringValue: String,
     val autoRecognitionStatus: MeasurementAutoRecognized,
     val recognitionId: String,
     val method: MeasurementMethod,
     val userKey: String,
     val userName: String,
     val isInRange: Boolean,
     var pictureUrl: String? = null,
     var remotePictureUrl: String? = null
)

If you need the secondary constructor for Java, you can annotate the given constructor with @JvmOverloads.

Upvotes: 6

Related Questions