Complete the code to declare a singleton object named Logger.
object [1] {
fun log(message: String) {
println(message)
}
}class keyword.The keyword object followed by the name Logger declares a singleton object in Kotlin.
Complete the code to call the log function on the singleton object.
fun main() {
[1].log("Hello Singleton")
}Singleton objects are accessed by their name directly without parentheses.
Fix the error in the code to ensure only one instance of the object is used.
val logger1 = [1]
val logger2 = Logger
println(logger1 === logger2)Singleton objects in Kotlin do not use parentheses. Using Logger accesses the single instance.
Fill both blanks to create a singleton object with a property and access it.
object [1] { val version = [2] } fun main() { println(AppInfo.version) }
The object name is AppInfo and the property version is a string "1.0".
Fill all three blanks to define a singleton object with a function and call it.
object [1] { fun greet(name: String) = "Hello, [2]!" } fun main() { println(Greeter.greet([3])) }
\$name instead of $name for string interpolation.The object is named Greeter. The function uses string interpolation with $name. The call passes the string "Alice".