Urgent.News

What's breaking now, across thousands of outlets.

Tech

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

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…

In Kotlin, a nested class is simply a class defined inside another class, with no connection to any object of the outer class. By default, it functions as a namespace for the outer class. For instance, the code snippet `class OrderTracker(val orderId: String) { class DeliveryStop(val name: String, val etaMinutes: Int) }` demonstrates a nested class where `DeliveryStop` is not linked to any `OrderTracker` object. This allows `DeliveryStop` to operate independently.

However, when you introduce an inner class, the dynamics change significantly. An inner class is associated with a single instance of the outer class, enabling it to access and manipulate the outer object's members, including private ones. An example of an inner class is `StatusLabel` within `OrderTracker`, which can retrieve the `orderId` using `this @ OrderTracker`. This association allows the inner class to maintain a strong reference to the outer object, keeping it alive as long as the inner object exists.

The key distinction lies in memory management. In Java, inner classes are default and can be static to break this link. In contrast, Kotlin adopts a safer approach by defaulting to inner classes, which necessitates explicit marking as `inner` to achieve the same behavior. This results in the compiler generating additional bytecode for inner classes, primarily a hidden reference field (e.g., `this$0`) that points to the outer object.

Consequently, if an inner class holds onto an outer object reference, it can lead to memory leaks, particularly in scenarios where the inner class outlives the outer object's lifecycle, such as when added to an activity or object that persists beyond its expected duration.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

192.168.1.254 Login - Access Your Router Admin Page

192.168.1.254 is the last usable address in the 192.168.1.0/24 subnet, and a handful of TP-Link, Huawei and ZTE routers use it as their default gateway.

  • 192.168.1.254 is default gateway for TP-Link, Huawei, ZTE routers
  • Access router admin page by entering http://192.168.1.254
  • Default login credentials are typically "admin" for both username and password

Alibaba Cloud Dynatrace: monitor ECS, SLB and databases in multi-cloud

Your application runs on AWS, but the product catalogue, or the Chinese region, lives on Alibaba Cloud. When a user complains, the first question is whether the fault is on your side or the other…

  • Alibaba Cloud Dynatrace extension monitors ECS, SLB, and databases across multiple clouds
  • Supports six core services: ECS, SLB, NAS, NAT Gateway, CEN, VPN, Kafka, and ECI
  • Four database engines optional: RDS for MySQL, PostgreSQL, SQL Server, MongoDB

Shrink a PDF to Exactly the Size You Need — Without Uploading It Anywhere

Most PDF compressors ask you to pick "low / medium / high" and hope. Here's a client-side approach that starts at the highest quality and steps down only until the file fits — and your document never…

  • ToolRush's Compress PDF tool sets exact target file size
  • Works backward from goal, adjusts image resolution gradually
  • Runs in browser with Ghostscript, no file upload required

More from Tuesday 17 November →