5

how to add 2 or more constructors ?? i know the use of data class in kotlin, but i am not getting what exactly this keyword is in kotlin and why we have to put anything inside this?

public class Model {
    public String mId, mTitle, mDesc;

    public Model() {

    }

    public Model(String mId, String mTitle, String mDesc) {
        this.mId = mId;
        this.mTitle = mTitle;
        this.mDesc = mDesc;
    }

    public String getmId() {
        return mId;
    }

    public void setmId(String mId) {
        this.mId = mId;
    }

    public String getmTitle() {
        return mTitle;
    }

    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public String getmDesc() {
        return mDesc;
    }

    public void setmDesc(String mDesc) {
        this.mDesc = mDesc;
    }
}

I know kotlin but not that much.

how i changed

data class model_for_single_row (val mId:String,val mTitle:String,val mDesc:String){
    constructor():this()
} 

it gives me error to put something inside this. why we use this here and why we should put, and what we should put?

3
  • 1
    data class model_for_single_row (val mId:String,val mTitle:String,val mDesc:String) Will create a class with getters (and setters, if var used) for params you provided. If you want to omit one or more of them, you can do so by making them nullable (and maybe provide a default value) data class MyClass(val name: String, val age: Int = null) then, usage would be val user = MyClass("Jerry") Commented Jun 30, 2020 at 7:43
  • 1
    check this link stackoverflow.com/questions/37873995/… Commented Jun 30, 2020 at 7:44
  • 1
    I think an empty / default constructor for a data class in Kotlin is somehow pointless... This kind of class is simply not meant to be initialized without initializing one or more of its attributes. But it may be possible, read about Secondary Constructors, please. Commented Jun 30, 2020 at 7:45

2 Answers 2

5

Default value of String in java is null, which isn't the case in Kotlin.

You can make fields nullable and attach their defualt values to null:

data class model_for_single_row(
    val mId: String? = null,
    val mTitle: String? = null,
    val mDesc: String? = null
)

You can call it like:

model_for_single_row()
model_for_single_row("id")
model_for_single_row("id", "title")
model_for_single_row("id", "title", "desc")
model_for_single_row(mTitle = "title")

Parameters not supplied will be null here.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, so there is no need to add constructor keyword with this line?
@Prabhatkumar the default values (nulls here) are assigned when not supplied.
0

Hope it could be useful, the this keyword is used concerning constructors inside a class :

1 - to delegate from a secondary constructor to the primary constructor, like this :

class Car(val id: String, val type: String) {
    constructor(id: String): this(id, "unknown")
}

2 - to delegate from a secondary constructor to another secondary constructor where no primary constructor is present; so here in this example there is a child class with more than one secondary constructor derived from a parent class with more than one secondary constructor and no primary constructor as well:

fun main(args: Array<String>) {

    val p1 = AuthLog("Bad Password")
}

open class Log {
    var data: String = ""
    var numberOfData = 0
    constructor(_data: String) {

    }
    constructor(_data: String, _numberOfData: Int) {
        data = _data
        numberOfData = _numberOfData
        println("$data: $numberOfData times")
    }
}

class AuthLog: Log {
    constructor(_data: String): this("From AuthLog -> + $_data", 10) {
    }

    constructor(_data: String, _numberOfData: Int): super(_data, _numberOfData) {
    }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.