Follow-up question to the previous one. I'm starti...
# codingconventions
f
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
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
you can also just put it below the class definition in the same file and avoid all this companion boilerplate.
f
@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
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