"val" versus "var" Declarations in Scala
Scala allows one to decide whether a variable is immutable or mutable. Immutable is Read only where as mutable is read-write. Immutable variables are declared with the keyword “val“.
scala> val age:Int=22
Here age has been initialized to a value during its declaration. As it has been declared as a val, age cannot be reassigned to a new value. Any attempt to do will result in al reassignment to val error. Let us consider an Array being declared as val.
scala> val names:Array[String]=new Array(6)
Here names reference cannot be changed to point to a different Array, but the array itself can be modified. In other words the contents/elements of the array can be modified.
scala> names= new Array(5) //This will give a reassignment to val error. scala> names(0)="Scala" //This does not give an error.
Also it is to be noted that val variables must be initialized when they are declared.
Mutable variables are delcared with the keyword “var“. Unlike val, “var” can be reassigned to different values or point to different objects. But they have to be initialised at the time of declaration.
scala> var age:Int=22 age: Int=22 scala> age=35 age: Int=35
There’s an exception to the rule that one must initialize the val’s and var’s. When they are used as constructor parameters, the val’s and var’s will be initialised when the object is instantiated. Also, derived classes can override val’s declared inside the parent classes.
No related posts.
4 Responses to "val" versus "var" Declarations in Scala
Connect to us …
Archives
- May 2013 (8)
- April 2013 (6)
- March 2013 (6)
- January 2013 (5)
- November 2012 (2)
- September 2012 (1)
- July 2012 (5)
- June 2012 (1)
- May 2012 (4)
- April 2012 (7)
- March 2012 (2)
- February 2012 (4)
- December 2011 (2)
- November 2011 (4)
- October 2011 (2)
- September 2011 (1)
- August 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (1)
- April 2011 (1)
- February 2011 (4)
- December 2010 (3)
- November 2010 (2)
- September 2010 (2)
- August 2010 (3)
- May 2010 (2)
- March 2010 (6)
- December 2009 (1)
- November 2009 (3)
- July 2009 (6)
- June 2009 (3)
- May 2009 (1)
- April 2009 (6)
- March 2009 (1)
- January 2009 (1)
- December 2008 (8)
- November 2008 (5)
- October 2008 (6)
- September 2008 (4)
- August 2008 (8)
- July 2008 (19)
- June 2008 (29)
- May 2008 (27)
- April 2008 (11)
- March 2008 (8)
- February 2008 (22)
- January 2008 (3)
Send To Readmill
Send to ReadmillRecent Posts
- How to create ADF TreeTable programmatically?
- Book review: The Object-Oriented Thought Process
- How to show links in ADF Messages
- Runtime Polymorphism in Java
- Understanding RowKey values in ADF TreeTable
- Train Wreck Pattern – A much improved implementation in Java 8
- Converting a List into comma separated value string in Java
- A simple application of Lambda Expressions in Java 8
- Template Method Pattern- Using Lambda Expressions, Default Methods
- First look at Learning Play! Framework 2
Disclaimer
Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for your support.










[...] So the fields can be declared as either- var’s and val’s (Read about their difference here). And the methods are declared using the keyword- def. Java programmers might note this difference [...]
[...] A discussion of val versus var. [...]
[...] as the field declared with val keyword would have only a getField generated. We already know from here the difference between val and [...]
[...] a val. Variables declared as var can be reassigned where as those declared as val cannot be. Read here for more details on their difference. Lets make the name field in the above Person class to val and [...]