Skip to content

Commit ce57bca

Browse files
authored
Use object declaration
Instead of using a companion object and creating a lazy instance, just use an object declaration.
1 parent f973031 commit ce57bca

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/main/kotlin/Singleton.kt

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
class PrinterDriver() {
2-
fun print() = println("Printing with object: $this")
3-
4-
companion object {
5-
val instance: PrinterDriver by lazy {
6-
PrinterDriver().apply { println("Initializing with object: $this") }
7-
}
1+
object PrinterDriver {
2+
init {
3+
println("Initializing with object: $this")
84
}
5+
6+
fun print() = println("Printing with object: $this")
97
}
108

119
fun main(args: Array<String>) {
1210
println("Start")
13-
PrinterDriver.instance.print()
14-
PrinterDriver.instance.print()
15-
}
11+
PrinterDriver.print()
12+
PrinterDriver.print()
13+
}

0 commit comments

Comments
 (0)