What is the difference between getter/setter methods and constructor in Java?


Constructors

A constructor in Java is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have the same name as their class and, have no return type.

If you do not provide a constructor the compiler defines one on your behalf, which initializes the instance variables with default values.

You can also accept parameters through constructors and initialize the instance variables of a class using the given values, these are known as parameterized constructors.

Example

The following Java program has a class named student which initializes its instance variables name and age using both default and parameterized constructors.

 Live Demo

import java.util.Scanner;
class Student {
   private String name;
   private int age;
   Student(){
      this.name = "Rama";
      this.age = 29;
   }
   Student(String name, int age){
      this.name = name;
      this.age = age;
   }
   public void display() {
      System.out.println("name: "+this.name);
      System.out.println("age: "+this.age);
   }
}
public class AccessData{
   public static void main(String args[]) {
      //Reading values from user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the name of the student: ");
      String name = sc.nextLine();
      System.out.println("Enter the age of the student: ");
      int age = sc.nextInt();
      Student obj1 = new Student(name, age);
      obj1.display();
      Student obj2 = new Student();
      obj2.display();
   }
}

Output

Enter the name of the student:
Krishna
Enter the age of the student:
20
name: Krishna
age: 20
name: Rama
age: 29

Getter and setter method

While defining a POJO/Bean object (or, encapsulating variables of a class) we generally,

  • Declare all the variables of the as private.

  • Provide public methods to modify and view their values (since you cannot access them directly).

The method that is used to set/modify the value of a private instance variable of a class is known as a setter method and, the method that is used to retrieve the value of a private instance variable is known as a getter method.

Example

In the following Java program, the Student (POJO) class has two variables name and age. We are encapsulating this class by making them private and providing setter and getter methods.

If you want to access these variables you cannot access them directly, you can just use the provided setter and getter methods to read and write their values. The variables which you haven’t provided these methods will be completely hidden from the outside classes.

 Live Demo

import java.util.Scanner;
class Student {
   private String name;
   private int age;
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void display() {
      System.out.println("name: "+getName());
      System.out.println("age: "+getAge());
   }
}
public class AccessData{
   public static void main(String args[]) {
      //Reading values from user
      Scanner sc = new Scanner(System.in);
   System.out.println("Enter the name of the student: ");
   String name = sc.nextLine();
   System.out.println("Enter the age of the student: ");
   int age = sc.nextInt();
   //Calling the setter and getter methods
   Student obj = new Student();
   obj.setName(name);
   obj.setAge(age);
   obj.display();
   }
}

Output

Enter the name of the student:
Krishna
Enter the age of the student:
20
name: Krishna
age: 20

As you observe, the main difference between constructors and setter/getter methods is −

  • The constructors are used to initialize the instance variable of a class or, create objects.

  • The setter/getter methods are used to assign/change and retrieve values of the instance variables of a class.

raja
Published on 10-Sep-2019 15:07:17
Advertisements