Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Getter and Setter in Java | Example Program

Getter method in Java


A method which is used to retrieve/get the value of a variable or return the value of the private member variable is called getter method in Java. This method is also known as accessor methodFor every private variable, you should create a getter method. 

Depending on the access level you want to give to the variable, you can set the access modifier of its getter method. If you declare instance variables as private, you add public getter methods for each one.

Setter method in Java


A method which is used for updating or setting the value of a variable is called setter method in Java. This method is also known as mutator method. By using the setter method, you can modify the value of a variable. Just like with the getter method, you should create a setter method for every variable in the class.

Naming convention of Getter and Setter method in Java


The naming of setter and getter can be done by using the Java bean naming convention. They are as follows:

           getXxx() and setXxx() where Xxx is the name of the variable.
For example:
     If the variable is the type of int, you will declare like this:
         private int number;
then appropriate getter and setter will be like this:
         public getNumber()  { }
         public void setNumber(int number)  { }

Similarly,
If the variable is a type of boolean then
         private boolean rich;
         public getRich()  { }
         public setRich()  { }


 

Let’s take simple example programs to understand getter and setter methods clearly.

Java Getter and Setter method Example Programs


Program source code 1:
In this example program, simply we are making a class representing a Mensuration with private variables and a couple of getter and setter methods.


Here, we will calculate the circumference, area of the circle and area, the perimeter of the rectangle using the getter and setter method.

 

package gettersetterProgram; 
public class Mensuration 
{ 
  public double x, y, r; 
// Create getter method for each variable. 
    public double getX() 
    { 
       return x; 
    } 
public double getY() 
{ 
   return y; 
} 
public double getR() 
{ 
   return r; 
} 
// Create setter method for each variable in the class. 
 public void setX(double x) 
 { 
    this.x = x; 
 } 
 public void setY(double y) 
 { 
    this.y = y; 
 } 
public void setR(double r) 
{ 
  this.r = r; 
} 
// Now write logic of Rectangle and Circle. 
    public double areaRec() 
    { 
      double area = x*y; 
       return area; 
    } 
 public double perRec() 
 { 
    double per = 2*(x+y); 
     return per; 
  } 
 public double areaCircle() 
 { 
    double area = 3.14*r*r; 
     return area; 
 } 
 public double circumCircle() 
 { 
    double circumference = 2*3.14*r; 
    return circumference; 
  } 
 } 
public class MyTest 
 { 
  public static void main(String[] args) 
  { 
    Mensuration mens; // Creating reference. 
     men's = new Mensuration(); // Creating object.
 
// Set the values of the variables. 
     mens.setX(20.5); 
     mens.setY(30.5); 
     mens.setR(12.5); 
  double areaRec = mens.areaRec(); 
  double perRec = mens.perRec(); 
  double areaCircle = mens.areaCircle(); 
  double circumCircle = mens.circumCircle(); 

  System.out.println("Area of rectangle: " +areaRec); 
  System.out.println("Perimeter of rectangle: " +perRec); 
  System.out.println("Area of circle: " +areaCircle); 
  System.out.println("Circumference of circle: " +circumCircle); 
 } 
}

 

Output: 
       Area of rectangle: 625.25 
       Perimeter of rectangle: 102.0 
       Area of circle: 490.625 
       Circumference of circle: 78.


Key points:
1. If you define only the getter method, it can be made read-only.
2. If you define only the setter method, it can be made write-only. 
3. If you define both getter and setter methods, it can be made read-write. Let’s another example program related to these key points. 

Program source code 2:
In this example program, we will define only the getter method to read the value of the variables. We will declare a constructor to initialize the values of the variables. Let’s see the following source code step by step.

package gettersetterProgram;
 public class Employee 
   { 
    private String name; 
    private int id; 
    private String nCompany; 
    Employee(String name, int id, String nCompany)
    { 
     this.id = id; 
     this.name = name; 
     this.nCompany = nCompany; 
    } 
 public String getName()
 { 
   return name; 
 } 
public int getId()
 { 
   return id; 
 } 
public String getNCompany()
 { 
   return nCompany;
 } 
} 

 package gettersetterProgram;
 public class  ReadOnlyTest 
 { 
  public static void main(String[] args)
   { 
    Employee em = new Employee("Deep", 23435, "TCS"); 
    String name = em.getName(); // This method will read the value of the variable. Since the return type of this method is String. That's why we will store the returning value using a variable 'name' with data type String. 

    int id = em.getId(); // The return type of this method is an integer. 
    String nCompany = em.getNCompany(); 
    System.out.println("Employee's name: " +name); 
    System.out.println("Employee's id: " +id);
    System.out.println("Company's name: " +nCompany);
   } 
  }
Output:
        Employee's name: Deep 
        Employee's id: 23435 
        Company's name: TCS

Program source code 3:
In this program, we will define only the setter method to set the values of the variable.

package gettersetterProgram; 
 public class Addition
 { 
    private int x, y, z; 
    public void setX(int a)
    { 
      x = a; 
    } 
 public void setY(int b)
  { 
   y = b; 
  } 
 public void setZ(int c)
  { 
    z = c; 
  } 
 void sum()
 { 
   int s = x + y + z; 
   System.out.println("Sum: " +s); 
  }
 } 

 public class MySum 
 { 
 public static void main(String[] args) 
  { 
    Addition a = new Addition(); 
       a.setX(10); 
       a.setY(20); 
       a.setZ(30); 
       a.sum(); 
   } 
}
Output:
       Sum: 60

Thus, the setter method is used to update or set the value of a variable whereas the getter method is used to read the value of a variable.

Final words 
Hope that this tutorial has covered all the important points related to the getter and setter method in Java with simple practical example programs. We hope that you will have understood and enjoyed this tutorial. Practice all example programs to make command on this topic.
Thanks for reading!
Next ⇒ Encapsulation in Java⇐ PrevNext ⇒

The post Getter and Setter in Java | Example Program appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Getter and Setter in Java | Example Program

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×