Kotlin by examples: Class and Properties

Daniele Bottillo
3 min readFeb 3, 2017

--

I’ve been using Kotlin in the last few months and I’m very pleased with it! For people who don’t know it, it’s a language built by JetBrains (the company behind IntelliJ hence Android Studio) which compiles to the same JVM bytecode of Java.
It’s a very modern and nice language which can be used along side Java files so doesn’t require to convert an entire project.

In this blog post I’m gonna show you how to write in Kotlin what you usually write in an Android/Java project and why writing code with Kotlin is faster and nicer :=) .

Let’s start with a standard definition of a class in Java:

Pretty standard Java classes, we have a base abstract class called ‘Car’ and a proper class ‘Ford’ which extends Car.
In Kotlin the same code would be:

So the 15 java lines of code are just 3! In Kotlin you define constructors inline in the class definition and you have getters and setters for free based on the visibility of the fields (more on this later).
The ‘extends’ keyword of Java is replaced by just a ‘:’ and you can also inline the constructor of the abstract class (in this case: Car(“Ford”) ).

Constructors and also methods support default values, something that Java can’t do:

It’s also possible to have named field in the creation of an object, which solve one of the main problem of Java in my opinion: to understand the meaning of the fields used to create a new object.
So let’s imagine a Book class in Java with one parameter as a string:

What is “George” ? is the author? the name of the book? the owner of the book? In Kotlin you can do this:

Definitely better!

In Kotlin you can add the keyword ‘data’ at the beginning of a class, if you do, the compiler will add these methods for “free”:

  • equals()/hashCode() pair,
  • toString() of the form "Ford(name=Ford, model=Focus)",
  • componentN()
  • copy()

So with just one keyword you are getting a lot of code for free! I want to show you what componentN() functions means because it’s something that you can’t do in Java. The idea is that you can destruct the declaration of your class:

Also the copy method is very useful to create another object from a previous one:

In Kotlin a property can be var or val. You can think about val as a final field in Java (so it can’t be modified) and var a mutable property which cannot be null, if you want to have a mutable nullable property you need to define with “<T>?” expression:

You can safely access a property that can be null with the keyword ‘?’:

In Android most of the time you need to wait callbacks from the framework to create objects, which means they need to be defined null until the framework is ready and then every time you use those objects you need to check if it’s not null.
Kotlin solves this problem with the lateinit keyword for var properties:

Please note that you don’t need to do ‘name?’ but just “name’ because the lateinit keyword tells the compiler that you are gonna fill the property before using it!

That’s it for now! In the next post I will talk about function definitions, return types, callback and static fields.

[1st Edit]: As for one of our comment, when I said that you can’t do <that> in Java it’s slightly inaccurate: because Kotlin does that for you doesn’t mean you can’t do the same in Java, the difference is that as per language, Kotlin offers you constructors, keywords, etc… that Java simply doesn’t. So for example you don’t have default values in constructors in Java, but you can create a Builder class which will function the same as that.

--

--

Daniele Bottillo

Android@Monzo Bank (London), technology enthusiast, book reader, TV shows fan, game player, Lego addicted.