memory wise, are companion object more efficient? ...
# getting-started
k
memory wise, are companion object more efficient? will all class instances share a single companion object or is a separate one created for every instance?
s
The companion object is a singleton
n
all `object`s are singletons, including the
companion
kind
k
so if I have this inside a class constructor:
Copy code
val foobar = object {
  val x = "x"
}
all of the instances will share the same object?
d
Oh, that's an anonymous object.
☝️ 1
All instances will have a unique object.
k
oh, so every instanc will have it's uqnie thing
right, right! thanks!
s
if you want to declare a companion object, the syntax looks like this:
Copy code
class Foo {
  companion object {
    val x = "x"
  }
}
all instances of
Foo
will have a reference to that same companion object