Skip to content

Instantly share code, notes, and snippets.

@ben-ng
Created May 30, 2019 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ben-ng/6b99bb45056882ee0eb894a5cb869e65 to your computer and use it in GitHub Desktop.
Save ben-ng/6b99bb45056882ee0eb894a5cb869e65 to your computer and use it in GitHub Desktop.
object Foo {
var initialized = 0
// This only happens once regardless of how many times Foo.fetch() is called
initialized = initialized + 1
def fetch(): Integer = initialized
}
class Foo {
println(s"initialized: ${Foo.fetch()}")
}
new Foo // prints "initialized: 1"
new Foo // prints "initialized: 1"
new Foo // prints "initialized: 1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment