Class declaration in Java vs in Kotlin

kotlin vs java: which is better
kotlin vs java code comparison
kotlin vs java? - quora
kotlin vs java 2019
kotlin vs java stack overflow
kotlin vs java performance
java to kotlin
kotlin tutorial

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.


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.

Kotlin vs. Java: 9 Benefits of Kotlin for Your Business, in many cases, solving the same problems with fewer lines of code. This improves code maintainability and readability, meaning engineers can write, read, and change code more effectively and efficiently. Kotlin class is similar to Java class, a class is a blueprint for the objects which have common properties. Kotlin classes are declared using keyword class. Kotlin class has a class header which specifies its type parameters, constructor etc. and the class body which is surrounded by curly braces. Syntax of Kotlin class declaration


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

Combine Java with Kotlin in Android, Kotlin does not have checked exceptions. What Java has that Kotlin does not. Checked exceptions · Primitive types that are not classes; Static members · Non-​  Kotlin will allow you to declare this property without initializing it, and you can set the property value at some point after construction (either directly or via a function). It is the responsibility of the class itself as well as its users to take care not to read the property before it has been set, and Kotlin allows you to write code that reads name as if it were an ordinary, non-nullable property.


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)

Java vs. Kotlin, Every class that doesn't explicitly declare a parent class inherits from Any , which is as usual, so a variable of type ContentKind can not be null, unlike in Java. Where you would previously use static field in Java, in Kotlin you will now create an object, and put that field in the object. Interoperability with Java: Because Kotlin is 100% interoperable with Java, sometimes you will want to expose certain APIs or fields in a way that is nicer for Java to read. To do this, you can use the @JvmStatic annotation.


Will Kotlin Replace Java? (with Pros and Cons) – HowToCreateApps, Kotlin Overview with Android and Comparison of Kotlin and Java Java Code */ static int num1, num2; //explicit declaration num1 = 20; //use the Java program */ public class Address { private String street; private int  Kotlin Variable. Variable refers to a memory location. It is used to store data. The data of variable can be changed and reused depending on condition or on information passed to the program. Variable Declaration. Kotlin variable is declared using keyword var and val.


Comparison to Java, Discover five reasons why Kotlin is better than Java for Android development. We also but in most cases it's enough just to declare the class and its properties. In Kotlin, class declaration consists of a class header and a class body surrounded by curly braces, similar to Java. Class myClass { // class Header // class Body } Like Java, Kotlin also allows to create several objects of a class and you are free to include its class members and functions.


Data classes - Classes, If you compare a Kotlin class and a Java class that are performing the same work, then the Kotlin class will generally be much more concise, but there's one area in particular where Kotlin can seriously reduce the amount of boilerplate code you need to write: findViewByIds. At most one Java class (and as many Java interfaces as you like) can be a supertype for a class in Kotlin. Accessing static members Static members of Java classes form "companion objects" for these classes.