2
votes

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.

3
Try to avoid having links as reference since they tend to be outdated after some timeAndreas Mattisson
Actually it should just be 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
Okay. Duly noted.Sumanth Sugunendran

3 Answers

1
votes

in Kotlin, it implicitly creates getters and setters for the fields (which you had in Java as well). As these are public, the fields themselves become effectively public.

Effectively your Java code with the simplistic getters and setters was the equivalent of having public fields because of the nature of the getters and setters (no validation etc.).

Had your setters done for example null checks and thrown IllegalArgumentExceptions, the code'd have been different.

0
votes

public member in Kotlin is not equivalent to public member in Java. It is still invisible to public when accessed by other Java class. You need to add @JvmField in front of the var to make it equivalent to public member in Java.

For Kotlin class Foo { var bar = 1 }. To access it by Java, new Foo().bar does not compile. You have to use new Foo().getBar(). bar is still a private member with getter and setter in the perspective of Java.

Changing the Kotlin code to class Foo { @JvmField var bar = 1 }, it truly becomes a public member in Java. You can then access it by Java using new Foo().bar

0
votes

There are getters, in cases with val, setters in case var. Access to the field for reading or changing always passes through them. You can notice them when using the Kotlin class from the java class.

If getters or setters describe the default behavior, then point them in the code does not make sense.

PS: if you convert your java class to Kotlin class, will like

class Person(var name: String?, var age: Int)