How does `::prop.isInitialized` work, and from where can you call it?
answer
- ::prop.isInitialized returns Boolean, never throws
- Only for lateinit, not lazy/notNull
- Callable from inside the declaring class scope
- Can't probe another instance from outside
- Used for idempotent setup and safe teardown
basics
~10 s::prop.isInitialized checks whether a lateinit property has been assigned yet, returning true or false without throwing. You usually call it from inside the same class using this::prop.isInitialized to avoid an exception before first use.
solid answer
~40 sFor a `lateinit` property you can query its assignment state via a property reference: `::prop.isInitialized` (or `this::prop.isInitialized`). It returns a `Boolean` and never throws, letting you safely branch before first assignment. The access is restricted: `isInitialized` is only available on the property reference from a scope where the property is accessible — typically from inside the declaring class (or a lexically enclosing scope, like a nested function). You cannot call `other.::field.isInitialized` on an arbitrary instance from outside; the check is meant for the owner. It only exists for `lateinit` properties — regular or delegated properties don't expose it. Common uses: idempotent setup (`if (!::cache.isInitialized) cache = build()`), and guarding cleanup so you don't touch an unassigned resource. Overusing it often signals a design smell where the lifecycle should be modeled more explicitly.
code
kotlin · 11 linesclass Resource {
lateinit var handle: FileHandle
fun open(h: FileHandle) { handle = h }
fun close() {
if (::handle.isInitialized) { // safe, no exception
handle.release()
}
}
}go deeper
Knows ::prop.isInitialized tells you if a lateinit was assigned without throwing.
Uses it correctly for idempotent setup/teardown and knows it's lateinit-only.
Explains the scope restriction (owner-only) and contrasts it with Lazy.isInitialized().
Treats heavy isInitialized use as a lifecycle smell and weighs lazy/constructor injection alternatives that remove the half-built state.
## The `isInitialized` check Kotlin lets you ask whether a `lateinit` property has been assigned, using the **bound property reference** syntax: ```kotlin class Cache { lateinit var data: Map<String, Int> fun getOrLoad(): Map<String, Int> { if (!::data.isInitialized) { // true once assigned data = loadFromDisk() } return data } } ``` `::data` is a reference to the property `data`; `.isInitialized` is an extension available specifically on references to `lateinit` properties. It returns a `Boolean` and **never throws**, so it's the safe way to test state before the first read. ## Where you can call it The key restriction: `isInitialized` is accessible **only from within a scope where the property is lexically visible** — in practice, inside the declaring class (including its nested/inner classes and local functions). You access it through: - `::prop` (implicit `this`) inside a member function, or - `this::prop` to be explicit. You **cannot** write `someInstance::prop.isInitialized` from outside the class to inspect another object's lateinit field. The feature is designed for the owner to manage its own lifecycle, not for external probing. ## What it works on - Only **`lateinit`** properties. Regular properties, `lazy` delegates, and `Delegates.notNull()` do not expose `isInitialized`. ## Typical uses ```kotlin // 1. Idempotent / lazy-ish setup if (!::connection.isInitialized) connection = open() // 2. Safe teardown fun close() { if (::connection.isInitialized) connection.close() } ``` ## Design note Reaching for `isInitialized` frequently is often a smell: it means the object can exist in a half-built state. Alternatives — constructor injection, `by lazy`, or a nullable with a smart contract — may express the lifecycle more honestly. But for framework-driven lifecycles (Android views, test fixtures) it's a pragmatic, legitimate guard.
- Can you call `other::field.isInitialized` to check another object's lateinit property from outside?No. `isInitialized` is only accessible via a property reference within the declaring class's scope; it isn't exposed for arbitrary external inspection of another instance.
- Does a `by lazy` property support `isInitialized`?Not through this syntax. `isInitialized` is specific to `lateinit`; `lazy` has its own `Lazy.isInitialized()` on the delegate object, which is different.
saying these in an interview costs you the question
- Saying isInitialized throws if not yet set (it returns false)
- Claiming you can check any instance's lateinit from outside
- Thinking it works on lazy or notNull delegates the same way
- Forgetting the `::` property-reference syntax
- Believing it auto-initializes the property