Variables in Scala

Before entering into the depth of OOP features, first, we need to know details about the different types of variables and data types in Scala. To declare a variable in Scala, you need to use var or val keywords. The formal syntax of declaring a variable in Scala is as follows:

val or var VariableName : DataType = Initial_Value

For example, let's see how can we declare two variables whose data types are explicitly specified as follows:

var myVar : Int = 50
val myVal : String = "Hello World! I've started learning Scala."

You can even just declare a variable without specifying the DataType. For example, let's see how to declare a variable using val or var, as follows:

var myVar = 50
val myVal = "Hello World! I've started learning Scala."

There are two types of variables in Scala: mutable and immutable that can be defined as follows:

  • Mutable: The ones whose values you can change later
  • Immutable: The ones whose values you cannot change once they have been set

In general, for declaring a mutable variable, a var keyword is used. On the other hand, for specifying an immutable variable, a val keyword is used. To show an example of using the mutable and immutable variables, let's consider the following code segment:

package com.chapter3.OOP 
object VariablesDemo {
def main(args: Array[String]) {
var myVar : Int = 50
valmyVal : String = "Hello World! I've started learning Scala."
myVar = 90
myVal = "Hello world!"
println(myVar)
println(myVal)
}
}

The preceding code works fine until myVar = 90, since myVar is a mutable variable. However, if you try to change the value of the immutable variable (that is, myVal), as shown earlier, your IDE will show a compilation error saying reassignment to val, as follows:

Figure 1: Reassignment of immutable variables is not allowed in Scala variable scope

Don't worry looking at the preceding code with the object and method! We will discuss classes, methods, and objects later in this chapter, then things will become more clear.

In Scala variables, we can have three different scopes, depending on the place where you have declared them:

  • Fields: These are variables that belong to an instance of a class of your Scala code. The fields are, therefore, accessible from inside every method in the object. However, depending on the access modifiers, fields can be accessible to instances of the other classes.
As discussed earlier, object fields can be mutable or they can be immutable (based on the declaration types using either var or val). But, they can't be both at the same time.
  • Method arguments: These are variables, and when the method is called, these can be used to pass the value inside a method. Method parameters are accessible only from inside the method. However, the objects being passed in may be accessible from the outside.
It is to be noted that method parameters/arguments are always immutable, no matter what is/are the keyword(s) specified.
  • Local variables: These variables are declared inside a method and are accessible from the inside the method itself. However, the calling code can access the returned value.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.135.138