Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Getters & Setters


class Human{

  String name;
  String food;
  String language;

  public void eat(){
    System.out.println("Eats "+food);
  }

  public void speak(){
    System.out.println("Speaks "+language);
  }
}

Although we have seen the methods of a class can be accessed using an object :


Human human = new Human();
human.eat();

i.e We have accessed the eat() method/behaviour using a human Object.

However, by the principles of Object Oriented Programming language, the properties of a class should not be accessed directly using an object.

i.e. If we create an object of Human class :


Human human = new Human();

and want to access the 'name' property of Human,


human.name

Accessing 'name' using 'human.name' is not at all a good practice. Rather we need to use a method/behaviour to access the 'name' property, popularly known as getter and setter methods.


Getter and Setter methods

The Setter method is used to set or assign values to a property(example:- name) of a class. And the Getter method is used to get the values of a property(example:- name) in a class.

Let us modify the 'Human' class using getter and setter methods :


class Human{

  String name;
  String food;
  String language;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getFood() {
    return food;
  }

  public void setFood(String food) {
    this.food = food;
  }

  public String getLanguage() {
    return language;
  }

  public void setLanguage(String language) {
    this.language = language;
  }

  public void eat(){
    System.out.println("Eats "+food);
  }

  public void speak(){
    System.out.println("Speaks "+language);
  }
}

Getter for 'name'

public String getName() {
   return name;
}

Setter for 'name'

public void setName(String name) {
   this.name = name;
}

Note :Do not worry about generating getters and setters yourself. IDE's are going to generate them for you.

How do we assign values to an object using Setter?

class CreateHuman{
  public static void main(String[] args){

    Human human = new Human();
    human.setName("John");
    human.setFood("Burger");
    human.setLanguage("English");

    human.eat();
    human.speak();

  }
}

Output :


  Eats Burger
  Speaks English

The above code uses the setter methods of name, food and language properties of Human class to set the values.


human.setName("John");
human.setFood("Burger");
human.setLanguage("English");

And eat() and speak() methods are used to specify the behaviors of what 'John' eats and speaks.


human.eat();
human.speak();

Below is how the values are assigned to the 'human' object.


java_object

How do we get the values of a class property using Getter ?

class CreateHuman{
  public static void main(String[] args){

    Human human = new Human();
    human.setName("John");
    human.setFood("Burger");
    human.setLanguage("English");

    System.out.println("His name is "+human.getName());
    System.out.println("He eats "+human.getFood());
    System.out.println("His language is "+human.getlanguage());

  }
}

Output :


  His name is John
  He eats Burger
  His language is English

In order to see the values of name, food and language properties, we have used the getter methods.


System.out.println("His name is "+human.getName());
System.out.println("He eats "+human.getFood());
System.out.println("His language is "+human.getlanguage());

It displays the values of name, food and language properties of Human class.