Getters and Setters in Scala - GeeksforGeeks

20 Jun, 2019
Getter and Setter in Scala are methods that helps us to get the value of variables and instantiate variables of class/trait respectively. Scala generates a class for the JVM with a private variable field and getter and setter methods. In Scala, the getters and setters are not named getXxx and setXxx, but they are used for the same purpose. At any time, we can redefine the getter and setter methods ourself.
Setter are a technique through which we set the value of variables of a class. Setting an variable of class is simple it can be done in two ways :-
First if the members of a class are accessible from anywhere. i.e no access modifier specified.Example:// A Scala program to illustrate // Setting the variable of a class  // Name of the class is Studentclass Student {    // Class variables    var student_name: String= " "    var student_age: Int= 0    var student_rollno= 0}   // Creating objectobject Main {    // Main method     def main(args: Array[String])     {        // Class object         var obj = new Student()         obj.student_name= "Yash"        obj.student_age= 22        obj.student_rollno= 59        println("Student Name: " + obj.student_name)         println("Student Age: " + obj.student_age)         println("Student Rollno: " + obj.student_rollno)            } }Output:Student Name: Yash
Student Age: 22
Student Rollno: 59For security reasons it is not recommended. As accessing the members of class directly is not a good a method to initiate and change the value as it will allow others to identify the variable.
// A Scala program to illustrate // Setting the variable of a class  // Name of the class is Studentclass Student {    // Class variables    var student_name: String= " "    var student_age: Int= 0    var student_rollno= 0}   // Creating objectobject Main {    // Main method     def main(args: Array[String])     {        // Class object         var obj = new Student()         obj.student_name= "Yash"        obj.student_age= 22        obj.student_rollno= 59        println("Student Name: " + obj.student_name)         println("Student Age: " + obj.student_age)         println("Student Rollno: " + obj.student_rollno)            } }
Output:
Student Name: Yash
Student Age: 22
Student Rollno: 59
For security reasons it is not recommended. As accessing the members of class directly is not a good a method to initiate and change the value as it will allow others to identify the variable.
Second if the members of a class are defined as private. Initiation of the variables is done by passing the variable to public method of that class using the object of the class.Example:// A Scala program to illustrate // Setting the private variable of a class  // Name of the class is Studentclass Student {    // Class variables    var student_name: String= " "    var student_age: Int= 0    private var student_rollno= 0          // Class method     def set_roll_no(x: Int)    {        student_rollno= x    }}   // Creating objectobject GFG{    // Main method     def main(args: Array[String])     {                   // Class object         var obj = new Student()         obj.student_name= "Yash"        obj.student_age= 22                  //error: variable student_rollno in class         // Student cannot be accessed in Student        //obj.student_rollno= 59         obj.set_roll_no(59)                  // Directly getting the value of variable        println("Student Name: "+ obj.student_name)                   // Directly getting the value of variable        println("Student Age: "+obj.student_age)                   // Through method calling        println("Student Rollno: "+obj.student_rollno)           } }
// A Scala program to illustrate // Setting the private variable of a class  // Name of the class is Studentclass Student {    // Class variables    var student_name: String= " "    var student_age: Int= 0    private var student_rollno= 0          // Class method     def set_roll_no(x: Int)    {        student_rollno= x    }}   // Creating objectobject GFG{    // Main method     def main(args: Array[String])     {                   // Class object         var obj = new Student()         obj.student_name= "Yash"        obj.student_age= 22                  //error: variable student_rollno in class         // Student cannot be accessed in Student        //obj.student_rollno= 59         obj.set_roll_no(59)                  // Directly getting the value of variable        println("Student Name: "+ obj.student_name)                   // Directly getting the value of variable        println("Student Age: "+obj.student_age)                   // Through method calling        println("Student Rollno: "+obj.student_rollno)           } }
Getters are a technique through which we get the value of the variables of a class.
Getting the value of a global variable directly. In which we call specify the name of the variable with the object.
Getting the value of a variable through method calling using the object. This technique is good when we don’t have accessibility to class variables but methods are available public.Example:// A Scala program to illustrate // Getting the value of members of a class  // Name of the class is Studentclass Student {    // Class variables    var student_name: String= " "    var student_age: Int= 0          // Getter    private var student_rollno= 0          // Class method     def set_rollno(x: Int){        student_rollno= x    }    def get_rollno(): Int ={        return student_rollno    }      }   // Creating objectobject Main {    // Main method     def main(args: Array[String])     {                   // Class object         var obj = new Student()         obj.student_name= "Yash"        obj.student_age= 22        obj.set_rollno(59)                  // Directly getting the value of variable        println("Student Name: " + obj.student_name)                   // Directly getting the value of variable        println("Student Age: " + obj.student_age)                   // Through method calling        println("Student Rollno: " + obj.get_rollno)     } }Output :Student Name: Yash
Student Age: 22
Student Rollno: 59

// A Scala program to illustrate // Getting the value of members of a class  // Name of the class is Studentclass Student {    // Class variables    var student_name: String= " "    var student_age: Int= 0          // Getter    private var student_rollno= 0          // Class method     def set_rollno(x: Int){        student_rollno= x    }    def get_rollno(): Int ={        return student_rollno    }      }   // Creating objectobject Main {    // Main method     def main(args: Array[String])     {                   // Class object         var obj = new Student()         obj.student_name= "Yash"        obj.student_age= 22        obj.set_rollno(59)                  // Directly getting the value of variable        println("Student Name: " + obj.student_name)                   // Directly getting the value of variable        println("Student Age: " + obj.student_age)                   // Through method calling        println("Student Rollno: " + obj.get_rollno)     } }
Output :
Student Name: Yash
Student Age: 22
Student Rollno: 59

Picked
Scala
Scala-Method
Scala-OOPS
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Scala Tutorial – Learn Scala with Step By Step Guide
Scala Map
Scala Lists
Scala List contains() method with example
Scala String replace() method with example
Scala | Arrays
How to get the first element of List in Scala
Anonymous Functions in Scala
HashMap in Scala
Enumeration in Scala