Complete the code to declare a named companion object called Factory.
class MyClass { companion object [1] { fun create(): MyClass = MyClass() } }
The named companion object is declared by giving a name after the companion object keyword. Here, Factory is the name.
Complete the code to call the create() function from the named companion object Factory.
val instance = MyClass.[1].create()To access a named companion object, use the class name followed by the companion object's name. Here, Factory is the name.
Fix the error in the code by completing the companion object declaration with the correct name.
class User { companion object [1] { fun newUser() = User() } } val u = User.[1].newUser()
The companion object must have the same name in both declaration and usage. Here, Factory is the correct name to fix the error.
Fill both blanks to create a named companion object called Helper and call its greet() function.
class Greeter { companion object [1] { fun greet() = "Hello" } } val message = Greeter.[2].greet()
The companion object is named Helper and accessed by the same name to call greet().
Fill all three blanks to define a named companion object Creator with a function build(), then call it.
class Product { companion object [1] { fun build() = Product() } } val p = Product.[2].[3]()
The companion object is named Creator. The function build() is called from it.