top of page
Click here to go to the home page of AskTheCode.

Implementation of Getter and Setter in Java - AskTheCode

Team ATC

Updated: Jul 2, 2021

Getter and Setter in Java explained with example | Java Programming Blog | AskTheCode

 

Get and Set methods, generally known as getters and setters in Java, are the methods used to get and set a value of a protected or encapsulated variable, respectively. Getters are sometimes also referred to as accessors, and Setters are termed as mutators.


Why and when to use getters and setters methods in java?

Getter used as get() in our java program, is implemented to fetch a value from an encapsulated variable of a class.

Setter generally coded as, set() in a java program, is used to set a value for an encapsulated variable of a class.

We can implement the get and set methods when there’s a situation to manipulate or to fetch data from a protected variable of a class.


How to use getter and setter methods in the java program?

Let’s see and analyze a case where we’ll need to implement the getter and setter.

Suppose, we want to create a student object from the Student class, and then want to give this student object a name, let’s say “Sam”.

class Student{
   private String name;
   // Getter
   public String get() {
      return name;
   }
   // Setter
   public void set(String name) {
      this.name = name;
   }
}

But, the name variable in the Student class is encapsulated, so, here if we try to access and manipulate this private variable using the normal approach of assigning a value, as shown below:

public class Getter_Setter_Example{
    public static void main(String[] a){
   	Student student = new Student ();

   	student.name = "Sam";
   	System.out.println("Student's name: " + student.name);
    }
}

You will get the output as:

Getter_Setter_Example.java:5: error: name has private access in Student
               student.name = "Sam";
                      ^
Getter_Setter_Example.java:6: error: name has private access in Student
               System.out.println("Student's name: "+student.name);
                                                            ^
2 errors

private keyword is used with the variable name, in the Student class. Thus, name can not be accessed from outside the Student class. If the variable name would be public, then we would get the output as:

Student's name: Sam

As we have discussed above that to access the private variable of a class, we implement get() and set(). Let's see how we could implement and use the same in this case.

We'd already written the getter and setter in the Student class as get() and set(). We can use these methods to get or set the value of name.

public class Getter_Setter_Example{
    public static void main(String[] a){
   	Student student = new Student ();

   	student.set("Sam");
   	System.out.println("Student's name: " + student.get());
    }
}

*Now, we will get the result as expected.

Recent Posts

See All

Comments


bottom of page