Objects

Objects

Objects are single instances of their own definitions.
1
object IdFactory {
2
private var counter = 0
3
def create(): Int = {
4
counter += 1
5
counter
6
}
7
}
8
val newId: Int = IdFactory.create()
9
println(newId) // 1
10
val newerId: Int = IdFactory.create()
11
println(newerId) // 2
Copied!
Last modified 1yr ago
Copy link