Kotlin classes

kotlin Mar 6, 2021

This is the second post in the "Kotlin, the essence of Java" series. Click the link for the whole story or keep on reading about this particular topic.

Classes are the core of Kotlin, just like in Java. In this post I will take a Java class and turn it into a Kotlin class without the bloat but with the same (if not better) functionality.

The example class I will use for this post is a pretty basic Person class. In Java:

class Person {
	private String name;
	private LocalDate birthDate;
	private String userID;

	public Person(String name, LocalDate birthDate, String userID) {
		this.name = name;
		this.birthDate = birthDate;
		this.userID = userID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public LocalDate getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(LocalDate birthDate) {
		this.birthDate = birthDate;
	}
	
	public String getUserID() {
		return userID;
	}
}

Nothing fancy, just

  • three fields
  • getters and setters
  • a constructor for all fields

Now the same class in Kotlin:

class Person(
	var name: String,
	var birthDate: LocalDate,
	val userID: String
)

How can this be the same thing!? Let's break it down.

Constructors

Java and Kotlin both use the class keyword and the class name to define a class. But after that, things start to differ. In Java, everything is inside the body of the class. That includes the constructor, which in Java is a separate function. Like so:

	public Person(String name, LocalDate birthDate, String userID) {
		this.name = name;
		this.birthDate = birthDate;
		this.userID = userID;
	}

In Kotlin however, the class declaration and the definition of the constructor are the same:

class Person(
	var name: String,
	var birthDate: LocalDate,
	val userID: String
)

As you can see, this makes the Kotlin class and constructor a lot smaller.

The Kotlin class is smaller than the Java constructor.

This is how the constructors differ:

Java Kotlin
The constructor is a separate, public function By convention the main constructor is public
It has the same name as the class The class name is followed by the constructor
It has parameters that duplicate the class' fields Its parameters are the fields
It assigns all those fields from the parameters No constructor body in this simple case

The Kotlin class is smaller than the Java constructor. That's a lot of redundancy which we accepted over the years!

Creating objects (calling the constructor)

Another example of Kotlin doing away with redundancy is the way you create new objects. In Java:

final Person person = new Person("Jim", LocalDate.of(2000, Month.JANUARY, 1), "justjim");

Remember that the constructor in Java is a function with the same name as the class? Kotlin sees you calling the constructor and doesn't need the new keyword to know that you want to create an object:

val person = Person("Jim", LocalDate.of(2000, Month.JANUARY, 1), "justjim")

Accessing fields (getters & setters)

The Java class has no setter for the userID field indicating it is read-only. This only works from outside of the Person class. Internally, userID can be changed as much as you want. (yes, you can make it final)

Kotlin uses val and var to indicate whether fields are writable or not:

class Person(
	var name: String, // read/write
	var birthDate: LocalDate, // read/write
	val userID: String // read-only
)

That's it! Now even the compiler won't let you change the value of userID.

Accessing the fields in Kotlin is done purely by field name, no need to call get...() or set...(...)

val me = Person("Sander", LocalDate.now(), "sander")
print(me.name) // accessing the 'getter'
me.name = "Sander Verbruggen" // accessing the 'setter'
me.userID = "sverbruggen" // gives a compilation error, because userId is a 'val' and cannot be reassigned

Next steps

Now that you know how Kotlin classes and objects work, you can start using Kotlin to write your domain or value classes. If the rest of your software is written in Java, take a look at Kotlin and Java interoperability to see how to integrate them.

And of course you can continue with the other posts in the Kotlin, the essence of Java series.

Tags