+ 4
Like this? object E { const val v = 2 } class T() { companion object { const val d = 5 const val n = E.v } }
13th Jun 2020, 12:51 AM
John Wells
John Wells - avatar
+ 3
Constants must be known at compile time and can not be instance based.
13th Jun 2020, 12:55 AM
John Wells
John Wells - avatar
+ 3
You can even do math, as long as it can be calculated by the compiler, like: class N() { companion object { const val t = T.d+T.n } }
13th Jun 2020, 1:25 AM
John Wells
John Wells - avatar
+ 2
If the constant object or its properties have the correct data type for the assignment, they can be used. That is a read only operation so is perfectly fine usage of any constant. Do you have an explict example you are asking about?
12th Jun 2020, 7:54 PM
John Wells
John Wells - avatar