Hi Vidya,
As everybody said that VAR is used to assign Values which can be changed,And
once a value is assigned to val it can't be changed.
So val are implemented as final types in scala.They are like constants in other
languages.
Let us consider you've defined a object.
scala> class A
defined class A
scala> val b=new A()
b: A = a...@5ebac9
scala> b=new A()
<console>:6: error: reassignment to val
b=new A()
scala> b=b
<console>:6: error: reassignment to val
b=b
This is because Val is like constant once assigned a value using pattern
matching operator '=' it can't be reassigned but however you can change the
valued of the object .
ie., if A would have fields like var b:Int you can changed it by A.b.
Var have the similar properties like reftypes in java.They can be reassigned.
>From Mukund's Post you can also see that since var types are changeable we've
>a setter and getter method provided directly by the compiler,but for val you
>only have a gettter method.
Hope it helps.
Thanks,
RajkumarGoel.
www.twitter.com/rajkumargoel
________________________________
From: Vidhya Ranganathan <[email protected]>
To: [email protected]
Sent: Monday, 22 June, 2009 12:41:51 PM
Subject: [twincling] Var and Val in scala
Hi all,
Can somebody explain the difference between, Var and Val in Scala?
Regards,
Vidhya
------------------------------------
http://twincling.org/
http://twitter.com/twincling
----------------------------------------
Yahoo! Groups Links
Cricket on your mind? Visit the ultimate cricket website. Enter
http://cricket.yahoo.com
[Non-text portions of this message have been removed]