Menu

Inner vs Nested Class in Kotlin: What Is the Difference?
📰

Inner vs Nested Class in Kotlin: What Is the Difference?

DEV Community: kotlin·Mayank Kumar·19 days ago
#ixpuzELh
#devtokotlin#devto#dev#class#inner
Long article (1638 words) — no AI summary yet.
Reading 0:00
15s threshold

In short: a nested class is just a class written inside another class. It has no link to any outer object. Mark it inner and every instance keeps a hidden reference to the outer object. That lets it use the outer class's members, but it also keeps the outer object alive for as long as the inner one lives.

One keyword looks like a small change. In the bytecode, it adds a field to every object you create.

Have you ever added inner just to make a red error go away in Android Studio? It works, the code compiles, and you move on. But that keyword changes what your object holds in memory. In an Activity, that can be the difference between a clean screen and a leak.

This post covers what a nested class is, what inner adds, what the compiler generates for each, and when to use inner .

What is a nested class in Kotlin?

A nested class is a class declared inside another class. By default, it has no connection to any object of the outer class.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.