Introduction

Chapter 5: Object creation

Although Kotlin can be written in a purely functional style, it can also be written in object oriented programming (OOP), much like Java. In OOP, we need to create every object we use, or at least define how it ought to be created, and different ways have different characteristics. It is important to know what options do we have. This is why this chapter shows different ways how we can define object creation, and explains their advantages and disadvantages.

If you are familiar with the Effective Java book by Joshua Bloch, then you may notice some similarities between this chapter and that book. It is no coincidence. This chapter relates to the first chapter of Effective Java. Although Kotlin is very different from Java, and there are only morsels of knowledge that can be used. For instance, static methods are not allowed in Kotlin, but we have very good alternatives like top-level functions and companion object functions. They don’t work the same way as static functions, so it is important to understand them. Similarly, with other items, you can notice similarities, but the changes that Kotlin has introduced are important. To cheer you up: these changes are mostly to provide more possibilities or force better style. Kotlin is a powerful and really well-designed language, and this chapter should mainly open your eyes to these new possibilities.

results matching ""

No results matching ""