Is it possible to use visibility modifier to have ...
# getting-started
f
Is it possible to use visibility modifier to have a class which can be returned by a factory but that cannot be created otherwise? Thanks
s
Copy code
class Foo private constructor() {
  companion object {
    fun of(): Foo {
      return Foo()
    }
  }
}
f
Thanks, I'm gonna try it to see if I got that right 😀
It looks like I still have not really understood what a companion object is 😞 Oh, wait, that's the equivalent of "a collection of static methods" in this case right? And that would be why I could call the of() fun without having an instance of that object to start with 🤔
s
the companion object is essentially the closest thing Kotlin has to
static
within a class, yes
it is instantiated independently of actual class instances and is accessible from within any instance
I think it’s built when the class is loaded but I could be wrong about that
f
thanks ✔️