> ## 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 Top-Level Property

A top-level property in Kotlin is a property declared directly at the root of a `.kt` file, outside the scope of any class, interface, or object declaration. It is scoped to the package in which it is defined and is initialized when the file's corresponding generated class is loaded by the ClassLoader.

## Syntax

Top-level properties are declared using standard `val` (read-only) or `var` (mutable) keywords. They support explicit initialization, type inference, and custom accessors.

```kotlin theme={"dark"}
package com.example.core

// Read-only top-level property
val systemVersion: String = "1.0.0"

// Mutable top-level property
var activeConnections: Int = 0

// Compile-time constant top-level property
const val MAX_RETRIES: Int = 3

// Top-level property with custom accessors (no backing field generated)
var connectionMultiplier: Int
    get() = activeConnections * 2
    set(value) {
        activeConnections = value / 2
    }
```

## JVM Compilation Mechanics

Because the Java Virtual Machine (JVM) requires all fields and methods to be encapsulated within a class, the Kotlin compiler generates a facade class to host top-level properties. By default, if the file is named `Network.kt`, the compiler generates a `public final class` named `NetworkKt`.

The compilation behavior depends on the property declaration:

1. **Standard `val` and `var`**:

   The compiler generates a `private static` backing field. It then generates a `public static final` getter method (and a setter method for `var`) to control access to that field.
2. **`const val`**:

   The compiler generates a `public static final` field. No getter is generated. The value is inlined at the call site during compilation.
3. **Custom Accessors (without `field` reference)**:

   The compiler generates `public static final` getter/setter methods but omits the `private static` backing field entirely.

## Visibility Modifiers

Visibility modifiers applied to top-level properties dictate their accessibility across the codebase:

* `public` (default): The property is accessible from anywhere in the project.
* `internal`: The property is accessible from anywhere within the same compiled module.
* `private`: The property is accessible *only* within the specific `.kt` file where it is declared. The generated static field and methods are marked `private` in the bytecode.
* `protected`: Not allowed on top-level declarations.

## Java Interoperability

When accessing Kotlin top-level properties from Java code, developers must invoke the generated static accessors on the file's facade class.

```java theme={"dark"}
// Java code accessing properties from Network.kt
String version = NetworkKt.getSystemVersion();
NetworkKt.setActiveConnections(5);

// const val is accessed directly as a static field
int retries = NetworkKt.MAX_RETRIES;
```

### Modifying Java Interoperability

You can alter how the JVM compiles top-level properties using annotations:

* **`@file:JvmName("CustomName")`**: Placed at the very top of the `.kt` file (before the package declaration), this changes the name of the generated facade class.
* **`@JvmField`**: Instructs the compiler not to generate getters/setters, but instead expose the property as a `public static` field.

```kotlin theme={"dark"}
@file:JvmName("NetworkUtils")
package com.example.core

@JvmField
val defaultTimeout: Int = 5000
```

```java theme={"dark"}
// Java access after applying annotations
int timeout = NetworkUtils.defaultTimeout;
```

<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>
