What is the equivalent of Java static final fields in Kotlin?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Java and Kotlin are both robust programming languages utilized for developing a broad range of applications. Java has been a stalwart for developers, but Kotlin, now a first-class citizen in the Android ecosystem, has risen for its concise syntax and modern features. Among the most common constructs in Java is the static final field, often used to define constants. Here's an exploration of how Kotlin manages to provide similar functionality.
Static Finals in Java
In Java, static final is commonly used to create constants. A static field belongs to the class itself rather than an instance of the class, while a final field denotes immutability. Combining these keywords, a static final field ensures there’s exactly one constant value per class:
This ensures that STATIC_FINAL_VARIABLE is accessible without an instance of the class, and its value is immutable once initialized.
Equivalent in Kotlin
Kotlin does not directly have static fields since it embraces a more object-oriented approach. However, Kotlin supports object singletons and top-level declarations to achieve similar functionality.
Using companion object
Kotlin uses companion objects to mimic Java's static fields. The fields defined inside a companion object are tied to the class and can be accessed similarly to Java’s static members. You can also use const to achieve immutability, akin to Java's final.
Here, STATIC_FINAL_VARIABLE in Kotlin is the direct equivalent of Java's public static final int STATIC_FINAL_VARIABLE = 42;. It’s immutable and accessible without an instance of Example.
Using Top-Level Constants
A cleaner, often more idiomatic way in Kotlin is to declare constants at the top level. Kotlin allows for top-level declarations, so the constant can be placed directly in a file, outside of any class:
This top-level variable can be accessed from any other part of the code without requiring an enclosing class or singleton.
Key Differences
While companion object is one method to achieve similar functionality to static methods and fields in Kotlin, there are key differences and recommendations depending on use cases:
| Feature | Java: Static Final | Kotlin: Companion Object | Kotlin: Top-Level |
| Declaration Scope | Within the class | Within companion object | Top level in a file |
| Access | ClassName.FIELD | ClassName.FIELD | Directly by name |
| Need for Enclosing Class | Yes | Typically, for organization | No |
| Initialization | At compile-time if static final | Compile-time with const | Compile-time with const |
Additional Tips
- Companion Object Initialization: Keep in mind that a
companion objectis initialized lazily the first time it is accessed. This is unlike Java’s static initializers, which are executed during class loading. - Performance Considerations: Since Kotlin's
companion objectis technically just a singleton object, there might be performance nuances in Android environments. Top-level declarations might be faster for primitives, as they don’t require the overhead of creating a singleton object on initialization. - Use
constWisely: Theconstmodifier should be used only on primitives or String types suitable for compile-time constants in Kotlin. For other types, consider usingvalwithin acompanion object.
Conclusion
Kotlin's approach caters to modern programming paradigms, emphasizing conciseness and safety. The way it handles static-like constructs mirrors its philosophy of eliminating as many pitfalls and boilerplate as possible. While translating all concepts may not always be straightforward, Kotlin provides robust alternatives that often encourage better practices.
Related reading
- What is the equivalent of Java static methods in Kotlin?
- What is the equivalent of the C++ Pair<L,R> in Java?
- What is the equivalent of the C# 'var' keyword in Java?
- What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?
- What is the height of iPhone's onscreen keyboard?
- What is the iOS 5.0 user agent string?
- What is the fastest way in Java to get the amount of factors a number has
- What is the 'instanceof' operator used for in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.