Tuesday, 25 July 2017

Pojo class in kotlin

Converting Java pojo class is much easier in kotlin.

Java Code :

public static class Person {
    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

Then add an annotation data to the resulting class. This annotation means 
the compiler will generate a bunch of useful methods in this class:
equals/hashCode, toString and some others. The getPeople function should start to compile.

Kotlin Solution :

class Person(name:String, age:Int) {
  val name:String
  val age:Int = 0
  init{
    this.name = name
    this.age = age
  }
}

Pojo class in kotlin