Home » Scala language

Scope of Scala Variables

Scala variables scope: In programming, each variable has its own lifetime and block of code in which it can be used, this is known as the scope of the variable. In this tutorial on the scope of variables in Scala, we will learn about the scope of a variable in Scale and their lifetime with example code.
Submitted by Shivang Yadav, on July 23, 2019

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.

In Scala programming, there are three types of variables. They are:

  1. Fields
  2. Method Parameters
  3. Local variables

Fields

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.

For example:

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.

Code:

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

Method parameters

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.

Code:

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

Local variables

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)

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

Code:

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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.