I am new to Kotlin and I have the following doubt -
Using the Java to Kotlin converter (this Link), I converted the following Java code to Kotlin.
Java Class:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
Generated Kotlin Class:
class Person(name:String, age:Int) {
var name:String
var age:Int = 0
init{
this.name = name
this.age = age
}
}
But I don't understand how the Java Code and the generated Kotlin code are equivalent because the visibility modifiers of the class data members change from private(in Java) to public(in Kotlin).
I believe that if the visibility modifiers are preserved(the data members are declared private in Kotlin), getters and setters will have to be created in Kotlin too and only then should they be equivalent.
class Person(var name:String,var age:Int)
. Your Kotlin code has more boilerplate. Btw the converter is not perfect (at the moment) at all so better do it manually if you can. – Enzokie