Home > Back-end >  Should Getters and Setters in Kotlin be the same as Java in Android
Should Getters and Setters in Kotlin be the same as Java in Android

Time:04-28

This Model class has getters and setters implemented the same as in Java but now written in Kotlin

 class HousePost{
private var uid: String = ""
private var postImage: String = ""
private var rent: String = ""
private var description: String = ""
private var publisher: String = ""
private var location: String = ""
private var postId: String? = ""
private var rooms: String = ""
private var caption: String = ""
private var dateTime: String = ""
var expandable: Boolean = false

constructor()
constructor(
   uid: String,
   postImage: String,
   rent: String,
   description: String,
   location: String,
   postId: String,
   publisher: String,
   rooms: String,
   caption: String,
   dateTime : String
) {
   this.uid = uid
   this.postImage = postImage
   this.rent = rent
   this.description = description
   this.location = location
   this.postId = postId
   this.publisher = publisher
   this.rooms = rooms
   this.caption = caption
   this.dateTime = dateTime
   this.expandable = false
}
//Post Details getters
fun getUid() : String {
   return uid
}
fun getPostId (): String? {
   return postId
}
fun getPostImage():String {
   return postImage
}
fun getDescription():String {
   return description
}
fun getLocation():String {
   return location
}
fun getRent():String {
   return rent
}
fun getPublisher(): String {
   return publisher
}

fun getRooms(): String {
   return rooms
}
fun getCaption() : String {
   return caption
}
fun getDateTime() : String {
   return dateTime
}

//Post Details setters
fun setUid (uid: String) {
   this.uid = uid
}
fun setPostId(postId: String) {
   this.postId = postId
}
fun setPostImage(postImage: String) {
   this.postImage = postImage
}
fun setLocation(location: String) {
   this.location = location
}
fun setDescription(description: String) {
   this.description = description
}
fun setRent(rent: String) {
   this.rent = rent
}
fun setPublisher(publisher: String) {
   this.publisher = publisher
}
fun setCaption(caption: String) {
   this.caption = caption
}
fun setDateTime(dateTime: String) {
   this.dateTime = dateTime
}

}

This class is supposed to model data from my database. Is that the correct way in Kotlin? There is a No setter/field for class found error I'm getting. My recyclerview does not show the data at all, I assume is due to the error.

CodePudding user response:

In Kotlin, a property doesn’t require explicit getter or setter methods.

Properties in Kotlin classes can be declared either as mutable, using the var keyword, or as read-only, using the val keyword.

A class whose main purpose is to hold data in Kotlin these are called data classes and are marked with data. For example in your case the class can be define as below:

data class HousePost(
    val uid: String = ""
    val postImage: String = ""
    val rent: String = ""
    val description: String = ""
    val publisher: String = ""
    val location: String = ""
    val postId: String? = ""
    val rooms: String = ""
    val caption: String = ""
    val dateTime: String = ""
    val expandable: Boolean = false
)

The val keyword make the properties of this data class immutable. It means that you cannot change its properties after initialized.

To make a property mutable you can use var.

For example:

val housePost = HousePost() // This will use all the default value
housePost.expandable = true // Val cannot be reassigned

To make expandable mutable use var like so:

data class HousePost(
    .
    .
    .
    var expandable: Boolean = false
)

val housePost = HousePost()
housePost.expandable = true // Can be reassigned

Edit

You got the error(No setter/field for class found error) because you mark the class property private. You can simply fix it by removing private, the constructor, getters and setters in your current class.

There are other ways to define a property in a class. You will get to know how to use them as you learn.

class HousePost {
    var uid: String = ""
    var postImage: String = ""
    var rent: String = ""
    var description: String = ""
    var publisher: String = ""
    var location: String = ""
    var postId: String? = ""
    var rooms: String = ""
    var caption: String = ""
    var dateTime: String = ""
    var expandable: Boolean = false
}

Or this:

class HousePost (
    var uid: String = ""
    var postImage: String = ""
    var rent: String = ""
    var description: String = ""
    var publisher: String = ""
    var location: String = ""
    var postId: String? = ""
    var rooms: String = ""
    var caption: String = ""
    var dateTime: String = ""
    var expandable: Boolean = false
)

CodePudding user response:

You just simple do this. Remove the functions, it's not usable

class HousePost {
    private var uid: String = ""
    private var postImage: String = ""
    private var rent: String = ""
    private var description: String = ""
    private var publisher: String = ""
    private var location: String = ""
    private var postId: String? = ""
    private var rooms: String = ""
    private var caption: String = ""
    private var dateTime: String = ""
    var expandable: Boolean = false

    constructor()
    constructor(uid: String,
                postImage: String,
                rent: String,
                description: String,
                location: String,
                postId: String,
                publisher: String,
                rooms: String,
                caption: String,
                dateTime: String) {
        this.uid = uid
        this.postImage = postImage
        this.rent = rent
        this.description = description
        this.location = location
        this.postId = postId
        this.publisher = publisher
        this.rooms = rooms
        this.caption = caption
        this.dateTime = dateTime
        this.expandable = false
    }
}

CodePudding user response:

Kotlin provides default getters and setters for properties, you don't have to manually define them.

In Kotlin

var data: String = ""

is equivalent as

var data: String = ""
    get() = field
    set(value) {
        field = value
    }

because Kotlin compiler generates it for you. But if you only want to make your getter public but not setter, then you can override setter

var data: String = ""
    private set
    

You can do the same for the getter too.


This class is supposed to model data from my database. Is that the correct way in Kotlin?

No, that is not the correct way to define a model class in Kotlin. Kotlin provides data classes, whose main function is to hold data. Data class primary constructor needs at least one parameter.

So, your model class can be refactored like this

data class HousePost(
    var uid: String = "",
    var postImage: String = "",
    var rent: String = "",
    var description: String = "",
    var publisher: String = "",
    var location: String = "",
    var postId: String? = "",
    var rooms: String = "",
    var caption: String = "",
    var dateTime: String = "",
    var expandable: Boolean = false
)

In any case, if you need only private setters for any field, you can make that constructor property private, and then make a public property that delegates to this private field.

data class HousePost(
    private var _uid: String = "",
    var postImage: String = "",
    var rent: String = "",
    var description: String = "",
    var publisher: String = "",
    var location: String = "",
    var postId: String? = "",
    var rooms: String = "",
    var caption: String = "",
    var dateTime: String = "",
    var expandable: Boolean = false
) {
    val uid: String get() = _uid
}
  • Related