Android Kotlin Basics – Static Classes and Functions

About This Series

This “Andorid Kotlin Basics” blog series is all about fundamentals. We’ll take a look at the basics of building Android apps with Kotlin from the SUPER basics, to the standard basics, to the not-so-basics. We’ll also be drawing comparisons to how things are done in Kotlin vs. Java and some other programming languages to build Android apps (like C# and Xamarin or JavaScript/TypeScript for Hybrid implementations).

Check Out the Pluralsight Course!

If you like this series, be sure to check out my course on Pluralsight – Building Android Apps with Kotlin: Getting Started where you can learn more while building your own real-world application in Kotlin along the way. You can also join the conversation and test your knowledge throughout the course with learning checks through each module!

Watch it here: https://app.pluralsight.com/library/courses/building-android-apps-kotlin-getting-started/table-of-contents

Kotlin Static Classes

 
Using statics in object oriented design is not really a necessity, but something we find ourselves doing from time to time. This is especially true with newer trends that bring more functional style programming mixed with object-oriented. The first thing to note, is that Kotlin does not define entire static class as “static classes”. Instead, we use what is called an object declaration. It is also true that Kotlin does not use the static keyword at all. Even if you wanted a single static function/method in a non-static class, you need to create what is called a companion object within your class.

Let’s look at example of using statics to implement the Factory Pattern – which allows for an object to create the instance of itself in a lazy fashion, using static methods. To do this in Java, we would create a class and a static function that returns an instance of itself. Here’s an example of doing this in Java and in Kotlin

Dog.java

class Dog {
    public static Dog create() {
        return new Dog();
    }
}

Dog.kt

class Dog() {
    companion object Factory {
        fun create(): Dog = Dog()
    }
}

There are a few differences that happen when using these companion objects from our Kotlin classes within our Java code.
When creating a companion object, it creates an inner class with a static method on it in Java, so to invoke the above factory create function, you would call

Dog.Factory.create();

but in Kotlin, you can simple say

Dog.create()

Also, the name of the companion object is optional, so you could simply say:
Dog.kt

class Dog() {
    companion object {
        fun create(): Dog = Dog()
    }
}

But to call this in Java, you would say:

Dog.Companion.create();

If you REALLY don’t want to have to call the .Companion from your Java code, you can use the @JvmStatic tag like so:

class Dog() {
    companion object {
        @JvmStatic
        fun create(): Dog = Dog()
    }
}

Then you can use it in Java the same way you do in Kotlin:

Dog.create();

On the same note as using the companion object, you can also create regular object declarations at the package level, and use functions within those in a static manner:

// remember, Dog factories in real life 
// are awful, awful things, but in the context of Data
// they are okay.
object DogFactory {
    fun create(): Dog = Dog() 
}

And this can be called directly if the package is imported:

DogFactory.create()

Next Steps

Although this is the best solution for creating static classes and methods the same way Java does it, the standards of Kotlin actually suggest using a completely new pattern – package level functions. We’ll talk about that in the next post of the series!

Statics are also extremely useful for another feature that helps Kotlin stand out – Extension Functions and Properties. These are very similar to C# extension methods, but with even more features. We will also cover this subject in a later post.

If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile developer tips and tricks!

Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s