Sling Academy
Home/Kotlin/How to Use Nested and Inner Classes in Kotlin

How to Use Nested and Inner Classes in Kotlin

Last updated: November 30, 2024

Kotlin, a modern programming language, provides two special kinds of nested class declarations: nested classes and inner classes. Utilizing these can greatly enhance your code organization and maintainability by encapsulating functionality.

Nested Classes

A nested class in Kotlin is simply a class declared inside another class. By default, these nested classes do not hold a reference to the outer class instance. It is basically static with respect to its outer class, similar to static nested classes in Java.

Defining a Nested Class

class Outer {
    private val bar: Int = 1

    class Nested {
        fun foo() = 2
    }
}

In this example, the Nested class is written within the Outer class.

Creating an Instance

Because nested classes act similar to static classes, you can create an instance using the outer class name:

val demo = Outer.Nested().foo() // Calls Nested method

Notice that the nested class does not have access to the private members of the outer class.

Inner Classes

An inner class in Kotlin is a nested class that maintains a reference to an instance of its outer class. This allows access to the members of the outer class.

Defining an Inner Class

class Outer {
    private val bar: Int = 1

    inner class Inner {
        fun foo() = bar
    }
}

This Inner class is nested within Outer and is declared with the inner keyword, making it an inner class.

Creating an Instance

With inner classes, you create an instance in a different manner:

val demo = Outer().Inner().foo() // Accesses bar of the outer instance

Here, the Inner class can access bar due to its reference to the Outer class instance.

Use Cases

  • Nested Classes: Use them when the class is only logically derived yet does not require access to the outer class' instance.
  • Inner Classes: Ideal for scenarios where you need to access outer class members or to logically associate functionality with a particular instance context.

Conclusion

Knowing when and how to use nested and inner classes in Kotlin can significantly improve the modularity and logical organization of your code. Nested classes are appropriate when the association is more structural, while inner classes should be employed when the nested class communications need to access the containing class instance members.

Next Article: Object Expressions and Anonymous Classes for Dynamic Behavior in Kotlin

Previous Article: Static-Like Behavior with Companion Objects in Kotlin

Series: Kotlin Object-Oriented Programming

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin