AnDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
internal property in Kotlin is a class member or top-level property whose visibility is strictly scoped to the module in which it is declared. Any code residing within the same compiled module can access the property, while code outside the module cannot resolve the reference.
In Kotlin’s compilation model, a “module” is defined as a set of Kotlin files compiled together. This specifically includes:
- An IntelliJ IDEA module.
- A Maven project.
- A Gradle source set (note that
mainandtestare considered separate source sets, but Kotlin provides special compiler flags to allow tests to accessinternalmembers ofmain). - A set of files compiled with a single invocation of the
<kotlinc>Ant task.
Syntax
Theinternal modifier is placed before the val or var keyword. It can be applied at the file level or within a class/object declaration.
Access Rules and Inheritance
- Getters and Setters: By default, the getter and setter of an
internalproperty inherit theinternalvisibility. You can restrict the setter further (e.g., toprivate), but you cannot make it more permissive than the property itself. - Overriding: If an
internalproperty is declared in anopenclass, any subclass overriding that property must maintain at leastinternalvisibility. It cannot be restricted toprotectedorprivatein the subclass. If no visibility modifier is provided on the override, it implicitly inherits theinternalvisibility. - Constructors: If a property is declared directly in a primary constructor using
internal valorinternal var, the property is internal, regardless of the visibility of the class itself.
JVM Compilation and Name Mangling
Because the Java Virtual Machine (JVM) lacks a native visibility modifier equivalent to Kotlin’s module-level scope (Java’s default visibility is package-private, which is fundamentally different), the Kotlin compiler implementsinternal visibility using a workaround in the bytecode.
When compiled to the JVM, the backing field of an internal property is generated as private by default, exactly like standard Kotlin properties. However, the generated getter and setter methods are made public. To enforce the module restriction and prevent external Java code from inadvertently accessing these members, the Kotlin compiler applies name mangling.
The compiler appends the module name to the generated public getter and setter methods. For example, an internal property named config in a module named core will result in a generated getter method resembling getConfig$core().
While this prevents accidental access from external Java code, it does not provide cryptographic security; the mangled methods are technically public in the bytecode and can still be invoked via Java reflection or by explicitly calling the mangled name from Java.
Master Kotlin with Deep Grasping Methodology!Learn More





