Const vs val vs object vs top level

What is the correct way to define the constant?

The studio suggests the val can be const , but why do we need the object which will be in the memory all the time even if it is never used?

On another hand if we write vat STR = "some_str" each time the instance of the class is created the STR is created again.

We can put the constant into the companion. However studio suggests to add const...

Companion will require more bytecode, so it seams better to use object , which is not always needed. Also we can put it on the top level which blows up the brain because we are use to declare constants inside the class to limit the context. Here there is a constant without the scope and can also have const .

So we were arguing here may soon start the fight. Please answer what is the correct way to use these great tools?

This is English speaking forum. Please find Russian community forum or ask here: Telegram: Contact @kotlin_lang

your link doesn’t work as it points to tg://resolve?domain=kotlin_lang (Telegram).

I couldn’t answer the question, but this is my translation for whoever can:

What is the correct way to define the constant?

The studio suggests the val can be const, but why do we need the object which will be in the memory all the time even if it is never used?

On another hand if we write vat STR = "some_str" each time the instance of the class is created the STR is created again.

We can put the constant into the companion. However studio suggests to add const...

Companion will require more bytecode, so it seams better to use object, which is not always needed. Also we can put it on the top level which blows up the brain because we are use to declare constants inside the class to limit the context. Here there is a constant without the scope and can also have const.

So we were arguing here may soon start the fight. Please answer what is the correct way to use these great tools?

2 Likes

Thank you for translate!

Use a const val. const values are compile-time constants, which means the compiler copies the value to or inlines the value in the bytecode of the class that contains the expression in which the compile-time-constant is used. This is the easiest, and in terms of speed the most efficient way.

Where should the constants be declared? Put them in the minimal scope they are to be used with:

  • Are the constants only used with a specific class? Put them in the companion object.
  • Are the constants used with multiple types of your module? Put them at the top level in a (new) file.

Note that a companion object is also just an object. Compared to a “normal” object, there should be minimal to no differences in the bytecode. A companion object is only more open to the class it belongs to, so the class can use less visible members.

Now stop worrying about the amount of space and bytecode your constants use. Unless you have a clear indication that the amount of constants is causing problems, you are wasting your time trying to come up with a way to use them as efficiently as possible.

2 Likes

In fact, it works. It is not regular site though, just link to user group in Telegram.

I try to follow it in the browser (Safari) and it cannot open the page.

Probably some glitch of safari or restriction in your country. Telegram is blocked in Russia, though everyone (including government) use it anyway.

I am in America, but didn’t install Telegram on my computer (if there is a computer version of it).

It doesn’t matter, just want to mention that other people also may have trouble to follow this link.

It works for me and it probably should work for USA even without desktop client installed (yes, there is one).
Anyway, the group is mostly in Russian. There is also Russian channel in kotlin slack, though it has rather low activity.