O'Reilly logo

Hands-On Serverless Applications with Kotlin by Ameya Kulkarni, Hardik Trivedi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required