Object declaration

You know writing a singleton is easy, but it requires extra care if you want your singleton class to be thread-safe. Kotlin knows this and offers an elegant keyword, object, to solve this. With the use of this keyword, it creates an object, and this is known as object declaration. It is not an expression, so it cannot be assigned to a property. It's a declaration and used by its name directly.

Objects cannot have a constructor.

The object is also memory-efficient. An object will be only be created when the first invocation happens. And if the invocation never happens throughout the app session, then obviously the object will not be created and no memory allocation will take place.

So let's create a kotlin class, AWSProvider ...

Get Hands-On Serverless Applications with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.