Scope of Variables In Scala
Last Updated :
17 Feb, 2019
Variable declaration specifies the name of the variable that would be stored in the memory and memory can be accessed further with this name of variable. There are three types of scope for Scala variable.
- Fields.
- Method Parameters.
- Local Variables.
Let's discuss each of them in detail.
Fields
We can access these kind of variables from every method in the object & from outside the object if we declared them with the right access modifiers.A field may be
mutable or
immutable, they can define them using
‘var’ or
‘val’.
Example:
Scala
// Scala program of field
// scope for Scala variable
// class created with field
// variables x and y.
class disp
{
var x = 10.3f
var y = 7f
def windet()
{
println("Value of x : "+x)
}
println("Value of y : "+y);
}
object Example
{
// Main method
def main(args:Array[String])
{
val Example = new disp()
Example.windet()
}
}
Output:
Value of y : 7.0
Value of x : 10.3
Above example shows disp class is created with field variables x and y. These variables can be accessed inside a method and invoked this from an object by creating reference.Hence below is the output obtained.
Method Parameters
We use these variables when we want to pass a value inside the method when we call it. They can be accessed inside the method and outside method if there is a reference to the object from outside the method. these variables are
always mutable Using with
'val' keyword.
Example:
Scala
// Scala program of Method
// scope for Scala variable
// class created with Method
// variables s1 and s2.
class rect
{
def mult(s1: Int, s2: Int)
{
var result = s1 * s2
println("Area is: " + result);
}
}
object Area
{
// Main method
def main(args:Array[String])
{
val su = new rect()
su.mult(5, 10)
}
}
Output:
Area is: 50
Above Example shows
rect class
is created with
mult method
accepting two method parameter variables s1 and s2. Area object is created and rect method is invoking by passing the values to variables s1 and s2.Hence below is the output obtained.
Local Variables
These type of variables are declared inside a method and are accessible within it only.Using
'var' &
'val' keywords, these variables can be both
mutable & immutable.
Example:
Scala
// Scala program of Method
// scope for Scala variable
// class created with Local
// variables s1 and s2.
class Area
{
def mult()
{
var(s1, s2) = (3, 80);
var s = s1 * s2;
println("Area is: " + s)
}
}
object Test
{
// Main method
def main(args:Array[String])
{
val ex = new Area()
ex.mult()
}
}
Output:
Area is: 240
Above example shows class Area having local variables s1, s2 & s inside method mult.These variables are not accessible outside the method.Hence the output obtained is below.
Similar Reads
Variables in Scala Variables are simply storage locations. Every variable is known by its name and stores some known and unknown piece of information known as value. So one can define a variable by its data type and name, a data type is responsible for allocating memory for the variable. In Scala there are two types o
3 min read
How to Read Environment Variables in Scala? Scala stands for scalable language. It is an object-oriented language that provides support for functional programming approach as well. Everything in scala is an object e.g. - values like 1,2 can invoke functions like toString(). Scala is a statically typed language although unlike other statically
3 min read
Set in Scala | Set-1 A set is a collection which only contains unique items. The uniqueness of a set are defined by the == method of the type that set holds. If you try to add a duplicate item in the set, then set quietly discard your request. Syntax: // Immutable set val variable_name: Set[type] = Set(item1, item2, ite
3 min read
Scala Trait Traversable | Set-2 prerequisite- Scala Trait Traversable | Set-1In the previous Set we have seen some of the operations performed by the Class Taversable. Now, in this Set we will perceive some more operations. These operations are as follows: Conversion operations: The Conversion operations are toList, toSeq, toArray
8 min read
How to find instance of a value type in Scala? In this article, we will learn how to find an instance of a value type in Scala. "Instance of a value" refers to checking whether a variable holds a specific type of data or object in a programming language. Finding Instance of a Value Type in ScalaBelow are the possible approaches to finding instan
3 min read
Reassignment of val in Scala Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language that also provides support to the functional programming approach. In Scala, there are two ways of declaring variables. Using varUsing ValUsing var! as in VariableIt allows
2 min read