If I have a base class with few hundred fields (ba...
# announcements
k
If I have a base class with few hundred fields (basically constants, but not declared as static) will the derived classes also have hundreds of fields in memory? or will the JVM somehow optimize that?
d
Fields are inherited. So if you make an instance of the subclass it will still have space for the fields. After all, someone could still use the fields.
k
thanks! so for space saving I guess static fields are the way to go
d
Constants should be in an
object
regardless, yes
s
@Karlo Lozovina I’m curious what your use-case is…
Is it just about constants, like
Copy code
class MyClass {
    val CONST_1 = 3
}
vs
Copy code
class MyClass {
    companion object {
        val CONST_1 = 3
    }
}
k
@streetsofboston nothing too interesting, some code generation going on, trying to go lower on memory usage...
👍 1
it would be nice if static fields could be required as part of interfaces...
d
Kotlin does not have "static" for this very reason. Make an interface for those fields and implement that interface on the companion object.
☝️ 2
s
You could make a plain
object
or a
companion object
implement an `interface`…
k
huh, that could maybe work... I'd have to split things into multiple classes but still...
thanks for the idea