You are currently viewing Advantage of using object declaration for public static constants?

Advantage of using object declaration for public static constants?

I read on Stack Overflow, somewhere, that it is best to use an object declaration to contain fields that one would have declared as “public static final” in Java. Specifically, someone said it’s better than using a companion object.

Is this true? Everywhere else, I’ve read that the only difference between these approaches is that the companion object is initialized when the class loads, whereas the object declaration is instantiated lazily. In itself, is that much of a reason to prefer using an object declaration? Is there any other reason to prefer it to the companion object?

submitted by /u/sacheie
[link] [comments]