Introduction
Objects let you create a single instance with properties and functions to hold data or behavior, like a mini-toolbox you can use anywhere.
object ObjectName { // properties // functions }
object Logger { fun log(message: String) { println("Log: $message") } }
object Config { val maxUsers = 100 val appName = "MyApp" }
object Counter { var count = 0 fun increment() { count++ } }
object Greeter { fun greet(name: String) { println("Hello, $name!") } } fun main() { Greeter.greet("Alice") Greeter.greet("Bob") }
object keyword in Kotlin do?object keyword purposeobject keyword creates a singleton, meaning one single instance with properties and functions.Config in Kotlin?object keyword followed by the name and curly braces.object Logger {
fun log(msg: String) = println("Log: $msg")
}
fun main() {
Logger.log("Start")
}object Data {
var count: Int
fun increment() {
count++
}
}Cache that stores key-value pairs and provides a function getValue(key: String): String?. Which of the following is the best Kotlin object declaration?object creates a singleton Cache with a private mutable map and a function to get values.Pass the test and this pattern moves unknown