avatarGabriel Shanahan

Summary

The web content discusses object expressions in Kotlin, which are similar to Java anonymous classes but with unique features, and illustrates their use in Kotlin programming.

Abstract

The article "Object Expressions" delves into the concept of object expressions in Kotlin, presenting them as a powerful alternative to Java's anonymous classes. It explains that Kotlin's object expressions do not always require a super-type and can access variables from the enclosing scope. The article provides examples of using anonymous objects as return types and value types in local or private declarations, emphasizing that all members of an anonymous object are accessible through the function or property. It also points readers to further documentation for rules regarding public or private inline functions or properties. The content is part of the "Kotlin Primer" series, intended to facilitate Kotlin adoption in Java-centric organizations, and is supported by Etnetera a.s., with a call to action to join their team.

Opinions

  • The author suggests that Kotlin's object expressions are an improvement over Java's anonymous classes due to their flexibility and the ability to omit a super-type.
  • The article is positioned as an educational resource, indicating the author's commitment to promoting Kotlin within the developer community.
  • The inclusion of interactive Kotlin Playground examples reflects the author's belief in the importance of hands-on learning and practical demonstrations.
  • By providing a link to the "Kotlin Primer" series and a table of contents, the author shows an intention to guide readers through a structured learning path.
  • The acknowledgment of Etnetera a.s. and the invitation to join their team suggest a partnership and endorsement of the company's support for Kotlin development.

Object Expressions

Object expressions as equivalents of Java anonymous classes, with a twist.

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #KOTLIN FEATURE

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Object expressions are the equivalent of Java anonymous classes:

A super-type is not required (which is equivalent to inheriting from Any), and variables from the enclosing scope can be accessed:

Using anonymous objects as return and value types

When an anonymous object is used as a type of a local or private, but not inline, declaration (function or property), all its members are accessible via this function or property:

If this function or property is public or private inline, there are rules you can read about in the docs.

Go back to Object Declarations, jump to the Table of Contents, or continue to Companion Objects.

Join me in Etnetera
Kotlin
Java
Programming
Object Oriented
Object Expression
Recommended from ReadMedium