xxxxxxxxxx
class Quiz {
val question1 = "This is a question1"
companion object StudentProgress {
var total: Int = 10
var answered: Int = 3
}
}
fun main() {
println("${Quiz.answered} of ${Quiz.total} answered.")
}
xxxxxxxxxx
object MySingleton {
fun myMethod(): Int {
return 5
}
}
MySingleton.myMethod() // 5
xxxxxxxxxx
object MySingleton {
// Define properties and methods here
var data: String = "Hello, Singleton!"
fun printData() {
println(data)
}
}
// Access singleton object
MySingleton.printData() // Output: Hello, Singleton!
xxxxxxxxxx
object Singleton {
// Declare properties and methods here
}
fun main() {
val instance1 = Singleton
val instance2 = Singleton
println(instance1) // Prints: Singleton@1b6d3586
println(instance2) // Prints: Singleton@1b6d3586
println(instance1 === instance2) // Prints: true (both instances refer to the same object)
}