Creating POJO Class for Kotlin

Kotlin has been developed over JVM and hence it is fully compatible with JVM. Java POJO class stands for Plain Old Java Object (POJO) which is used to hold the data.

  • In Java, along with defining the variables, we need to create different supporting methods in order to access those private members of the class.

  • But Kotlin provides a unique way to declare a POJO class by introducing the "data" keyword. It can be applied along with class.

  • Once a class is defined as data class, the Kotlin compiler itself will be creating all the supporting getter() and setter() methods for this class.

Example – POJO using data class

In this example, we will see how we can create a POJO class of Student and how we can access the same from our main().

fun main(args: Array<String>) {
   // object initialization using default constructor
   val stud = student("name","location")
   //default getter() will be called
// creating data class
data class student (val name: String,val loc: String)


Once the above piece of code is executed, it will generate the following output −