Values and Mutability In Kotlin: Getting Started

In this Values and Mutability tutorial, you’ll learn how to declare mutable, immutable, constant, late/lazily initialized, static & inline values. By Gabriela Kordić.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Exposing Kotlin Static Properties in Java

The previous approach of defining properties is OK if you’re using Kotlin only. However, what if you need to use the companion object’s static values or methods from Java files? The answer is JVM annotations.

To make a property recognizable by the Java compiler, add a @JvmField annotation before the constant you want to use. In the case of a static method, add @JvmStatic before fun.

Note: If you’re interested in Kotlin-Java interop, check out Kotlin-Java interop in the Android documentation and static fields and methods in the Kotlin documentation.

Inlining Properties

You’re almost at the end! With technology growing, developers can experience huge runtime vulnerability because of memory allocation. The big problem lies in reserving space for functions, as a function is an object with a closure. And the closure is never empty — in 99 percent of the time, it contains a bunch of variables. Because of that, you can use inlining to save memory! The keyword for this is inline, and it needs to be set before fun.

When inlining properties, you have to inline their accessors. You can inline accessors individually, or you can place the keyword before the entire property to inline all accessors. As a result of mutability rules, for immutable properties you can only inline get().

Try to use it. Open MainActivity.kt and replace TAG with:

val TAG: String?
  inline get() = MainActivity::class.qualifiedName

In this block, you’re defining an immutable value that sets the Activity’s qualified name using an inline custom getter. For this example, you can’t inline the setter because of its mutability setting.

Now, find TODO in onItemSelected() and replace it with the following:

Log.d(TAG, "Selected item: ${viewModel.unitArray[p2]}")

Here you’re logging a selected item value in the console with a defined tag.

Run decompiling in the Kotlin bytecode tool. Press Decompile, and when the generated file opens, scroll to the place where you use TAG. Notice that with inline, the system evaluates operations at the call site. If you have a more complex expression in your application, use inlining properties for small performance optimizations.

Inlining Functions

When inlining functions, the most common use is on higher-order functions with lambda parameters. This avoids extra object creation of functional parameters because the system omits the method call and moves the method code to the call site. Consequently, the memory is less jammed.

Now, try inlining on a function. Open ConversionHelper.kt and add inline before getConvertedValue():

inline fun getConvertedValue(
  conversion: Conversion,
  temperatureValue: Double,
  finalUnit: String,
  onSuccess: (Double, String) -> Unit,
  onError: (Int) -> Unit
)

Here you’re only setting up getConvertedValue() to execute where you call it.

Build and run for the last time! Although you won’t experience any new functionality, play a bit with conversions to check that there are no bugs.

The main screen with all functionallities.

Where to Go From Here?

Download the final project by clicking the Download Materials button at the top or bottom of the tutorial.

Nice work on this tutorial! You’ve learned the best practice for declaring constants, as well as the importance of the companion object and singleton. Also, you’ve practiced how to define inline properties and how to clone Java static behavior in Kotlin.

If you’re wondering how to use collections and mutability, refer to this Kotlin Collections tutorial.

To learn more about the companion object, check out this Companion Objects video course.

If you’re interested in inlining, see the Kotlin documentation on inline functions and properties.

Hopefully, you’ve learned a lot and had fun! :]

If you have any questions or comments, please join the forum discussion below!