JetBrains has introduced experimental companion blocks and companion extensions in Kotlin 2.5.0-Beta1. These features aim to provide functionality similar to static members found in other programming languages, allowing developers to define constants, helper functions, and factory methods that belong to a class rather than an instance.
Companion blocks are defined within a class and compile into static members on the JVM, which improves compatibility for Kotlin Multiplatform projects. Companion extensions allow developers to add new members to any type, even if they do not control the original source code or if the class lacks a companion object.
To enable these features, developers must use specific compiler flags: -Xcompanion-blocks or -Xcompanion-blocks-and-extensions. Using the latter marks binaries as pre-release, preventing their use as dependencies until the feature stabilizes. Companion blocks alone do not impose this restriction on dependencies.
While companion blocks are recommended for new code, traditional companion objects remain supported. Companion objects are still required in scenarios where the companion must implement an interface, as they compile into full-fledged classes. Migrating to blocks requires recompiling the project and its dependencies.






