×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scope of Scala Variables

By IncludeHelp Last updated : October 09, 2024

Scala variables scope

Scope of the variable is the block of code until which the variable can be used within the scope of a variable. Any operation can be performed on it but accessing it outside the scope will give an error.

Example

def add(){
    var sum = 5+13
}
println(sum)

The above example will give an error as the scope of the sum is within the add() function and program are using it outside which is not allowed.

Types of Scope of Scala Variables

There are three types of Scala variables scope, they are:

  1. Fields
  2. Method Parameters
  3. Local Variables

1) Fields Scope

A field is a variable that is defined in a class ie. variables defined inside the class body are known as fields. A field can be used by any method that is defined inside the class, also so you can use the object of the class to access the fields. Based on their access modifiers the object is allowed to access the fields. These access modifiers allow or disallow any member or object or subclass to access the specified field.

Scala example of field scope

For class, there might be a private variable. this private variable can be accessed from any other method of the class but the object of the class will not have access to it.

class Student
{ 
	var roll = 2343;
	private var totalMarks = 500 // this cannot be accessed by the object
	def printinfo(mark : Int) 
	{ 
		println("The roll no. of student is "+roll)
		println("Percentage is "+(mark/totalMarks))
	} 
} 

object MyClass
{ 
	def main(args:Array[String]) 
	{ 
	    var s1 = new Student
	    println("call using . operator" + s1.roll)
	    // println("call using . operator" + s1.totalMarks) // will give error
 	    s1.printinfo(345)	    
	} 
} 

Output

call using . operator2343
The roll no. of student is 2343
Percentage is 0

2) Method Parameters Scope

Parameters are those variables which are passed to the method while calling the method. These variables can be accessed only inside the method in which they are defined.

For example:

def add(a:Int , b:Int){
	var sum = a+b
}
println(a)

As in this code, the variables a and b are parameters that are passed to the function add(). They cannot be used outside the block of the function. This means the println() line will give an error.

Scala example of method parameters scope

class Student
{ 
	var roll = 2343;
	private var totalMarks = 500 // this cannot be accessed by the object
	def printinfo(mark : Int) 
	{ 
		println("The roll no. of student is "+roll)
		println("Percentage is "+(mark/totalMarks)) // marks is a parementer passed to method
	} 
	def printmarks(){
	    println(mark)
	}
} 

object MyClass
{ 
	def main(args:Array[String]) 
	{ 
	    var s1 = new Student
	    println("call using . operator" + s1.roll)
	   // println("call using . operator" + s1.totalMarks) // will give error
 	    s1.printinfo(345)
	    
	} 
} 

Output


/home/jdoodle.scala:11: error: not found: value mark
	    println(mark)
                    ^
one error found
Command exited with non-zero status 1

3) Local Variables Scope

Variables are those variables which are r define inside a class And if there scope also lies within the method in which they are defined.

For example:

def add(a:Int , b:Int){
	var sum = a+b
}
println(sum)

Scala example of local variables scope

In this example, the code tries to access a variable outside its scope. This local variable cannot be used outside of the function.

class Student
{ 
	var roll = 2343;
	private var totalMarks = 500 // this cannot be accessed by the object
	def printinfo(mark : Int) 
	{ 
		println("The roll no. of student is "+roll)
		val percent = ((mark/totalMarks)*100)
		println("Percentage is "+(percent)) // marks is a parementer passed to method
	} 
	def printmarks(){
	    println(percent)
	}
} 

object MyClass
{ 
	def main(args:Array[String]) 
	{ 
	    var s1 = new Student
	    println("call using . operator" + s1.roll)
	   // println("call using . operator" + s1.totalMarks) // will give error
 	    s1.printinfo(345)
	    
	} 
} 

Output


/home/jdoodle.scala:12: error: not found: value percent
	    println(percent)
                    ^
one error found
Command exited with non-zero status 1

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.