https://kotlinlang.org logo
Title
f

Felipe Álvarez

10/09/2019, 3:29 PM
Follow-up question to the previous one. I'm starting with Kotlin migrating some classes. In Java, I try to define as
private static final
attributes to avoid memory footprint and object creation. In Kotlin, I created a companion object with a
private const val
but when decompiled code shows a new inner static class is created, does this create more memory footprint than the Java counterpart? Is there a better way to define constant which should be used only in one class?
k

karelpeeters

10/09/2019, 5:36 PM
It's more memory footprint, but there's no per object overhead if you're afraid of that. You can also do
@JvmStatic
on the companion object values if you've determined the overhead does matter for your application.
👍 1
g

ghedeon

10/12/2019, 6:37 AM
you can also just put it below the class definition in the same file and avoid all this companion boilerplate.
f

Felipe Álvarez

10/12/2019, 5:52 PM
@ghedeon that creates a class a class <FileName>Kt containing the
private static final
inside. I'm more worried about memory footprint than boilerplate. Although, I agree the whole
companion object
thing reminds me of Java verbosity
h

Hullaballoonatic

10/30/2019, 5:51 PM
yeah, kotlin has its own forms of verbosity not even seen in java, like
mutableListOf(...)
,
1.0 + 1.toDouble()
, and the aforementioned, but more than makes up for it in almost every other area in that regard