> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Private Property

A private property in Kotlin is a class member or top-level variable declared with the `private` visibility modifier, strictly confining its lexical scope and accessibility to the exact enclosing declaration.

## Syntax

The `private` modifier precedes the `val` (read-only) or `var` (mutable) keyword:

```kotlin theme={"dark"}
private val immutableData: String = "Static"
private var mutableState: Int = 0
```

## Scoping Rules

The exact boundary of a private property depends on its declaration context:

* **Class/Interface Level:** When declared inside a class or interface, the property is visible only within the `{}` block of that specific class. It is strictly invisible to external classes, instantiating code, and subclasses (unlike the `protected` modifier).
* **Top-Level (File Level):** When declared outside of any class, the property is visible to all functions, classes, and objects defined within that specific `.kt` file, but invisible to the rest of the module or package.

## JVM Compilation and Backing Fields

Under the hood, when compiling to the JVM, Kotlin handles private properties differently than public ones:

1. **Field Generation:** It generates a `private` backing field.
2. **Accessor Generation:** Unlike public properties, the Kotlin compiler does *not* generate public `get()` and `set()` methods.
3. **Direct Access:** Accessing the private property from within its allowed scope is typically optimized by the compiler into direct field access at the bytecode level, bypassing method invocation overhead unless custom accessors are defined.

## Custom Accessors

Private properties fully support custom getters and setters. The accessors implicitly inherit the `private` visibility of the property itself.

```kotlin theme={"dark"}
class DataContainer {
    private var internalCounter: Int = 0
        get() = field + 1
        set(value) {
            field = if (value > 0) value else 0
        }
}
```

## Companion Object Interaction

Kotlin enforces a specific visibility relationship between a class and its companion object regarding private members:

* A class can access the private properties of its `companion object`.
* A `companion object` can access the private properties of its enclosing class instances.

```kotlin theme={"dark"}
class OuterClass {
    private val instanceSecret = "Instance Level"

    companion object {
        private val staticSecret = "Companion Level"
    }

    fun readCompanion() {
        println(staticSecret) // Valid access
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
