because of the "
def",
a is a function that returns a new
Data every time it's called
the Java equivalent of
def a = new Datawould be
Data a() { return new Data(); }
So each of the following statements refers to a unique instance of
Data:
println(a.value)
a.value = "new value"println(a.value)
On Wed, Jul 8, 2009 at 12:35 PM, Christoph Drießen
<ced@...> wrote:
Hi all,
running the following code yields
null
null
null
new value
class Data {
var value: String = _
}
object DataTest {
def main(args: Array[String]) {
def a = new Data
val b = new Data
println(a.value)
println(b.value)
a.value = "new value"
b.value = "new value"
println(a.value)
println(b.value)
}
}
Can anyone explain the difference between def and val in this situation? Playing around with both I cannot find any difference between 'a' and 'b', they are both the same class. But 'a' must be something different. What exactly is it then?
Cheers,
Christoph