Kotlin Getters and Setters

In this tutorial you will learn about the Kotlin Getters and Setters and its application with practical example.

Kotlin Getters and Setters (Bean)

Beans are classes that encapsulate many objects into a single object. It contains constructor, Getter, Setter Methods. With the help of Setter and Getter method user can set and get data. It also have zero argument constructor.

In Kotlin we do not need to write getter and setter method in model , getter and setter methods are auto genarated in Kotlin . It is very simple to create model in Kotlin support.

Example: Let’s get understand about Bean in Kotlin. Create a bean class.

Now Create a new Class like below Image.

get-set-1

Check below code.

How to instantitate Object of Bean class : From the below code you can instantiate object.

Setting Value: With the help of below syntax you can set value to bean.

Getting Value: With the help of below syntax you can get and print value from bean.

Output: Let’s run this code and you will get output like below image.

get-set-2

In this tutorial we have learn about the Kotlin Getters and Setters and its application with practical example. I hope you will like this tutorial.