• If we want to access variables inside a class we can access them using object. object.variable_name; and if we want to change the value of variable we will assign using object.variable_name=value;
  • By using constructor we can assign some values to the class variables whenever object is created but this is one time initialization.

  • To assign some value to variable directly assigning value or get value of variable using object   is not recommended. 
  • Actually we need to use methods to perform any task. And to assign and to get values of a object we have setter and getter methods concept in java.
  • Getter and setter methods are like normal methods in java. but to initialize new value and get the value of instance variables we will use use these methods that is the reason behind specialty of these methods
  • We can set as well as get value from variables so these are called setter and getter methods.
  • so declare variables as private to prevent from accessing directly using object of the class. and use get and set methods in java like
  • setXXX() and getXXX  to assign values and access variables . SetXXX() and getXXX() here set and get are naming conventions to be used. where as XXX represent variable names.
  • If we observe this it is pure encapsulation in java.


Set Method  /  Setter method in java:

  • Purpose of Setter method is to set new value or assign new value to instance variable .
  1. Method name should follow naming convention setVARIABLENAME().
  2. It should accept some value as an argument. here method argument should be of type of variable.
  3. It should have a statement to assign argument value to corresponding variable.
  4. It does not have any return type. void should be the method return type.
  5. In order to set some value to variable we need to call corresponding setter method  by passing required value.



  1. package settterandgettermethods;

  2. public class SetAndGet {
  3.  private String name;
  4.  private int id;
  5.  

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

  9. public void setId(int id) {
  10. this.id = id;
  11. }

  12. }



Get method / Getter  method in java:

  • Purpose of Getter method is to get the value of the instance variable.

  1. Method name should follow naming convention getVARIABLENAME().
  2. It should not have any arguments.
  3. It should return corresponding variable value.
  4. So return type must be of type of variable we are returning from the method.
  5. In order to get the variable value we need to call corresponding getter method of variable.

  1. package settterandgettermethods;

  2. public class SetAndGet {
  3.  
  4.  private String name;
  5.  private int id;

  6. public String getName() {
  7.  return name;
  8. }

  9. public void setId(int id) {
  10.  this.id = id;
  11. }

  12. public static void main(String args[]){
  13.  
  14.  SetAndGet obj = new SetAndGet();
  15.  String name =obj.getName();
  16.  
  17. }

  18. }


Java program to get and set variable values without setter and getter methods

  1. package settterandgettermethods;

  2. public class SetAndGet {
  3.  
  4.  private String name;
  5.  private int id;


  6. public static void main(String args[]){
  7.  
  8.  SetAndGet obj = new SetAndGet();
  9.  
  10.  obj.name="setting some value";
  11.  obj.id=1;
  12.  System.out.println(obj.name);
  13.  System.out.println(obj.id);
  14. }


  15. }


Output:


  1. setting some value
  2. 1

Java program to get and set variable values with setter and getter methods

  1. package settterandgettermethods;

  2. public class SetAndGet {
  3.  
  4.  private String name;
  5.  private int id;


  6. public String getName() {
  7.  return name;
  8. }

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

  12. public int getId() {
  13.  return id;
  14. }

  15. public void setId(int id) {
  16.  this.id = id;
  17. }


  18. public static void main(String args[]){
  19.  
  20.  SetAndGet obj = new SetAndGet();
  21.  
  22.  obj.setName("java");
  23.  obj.setId(1);
  24.  System.out.println(obj.getName());
  25.  System.out.println(obj.getId());
  26. }


  27. }

Output:


  1. java
  2. 1


set and get methods in java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

3 comments for Setter and getter methods in java with example program

  1. Thanks for the post.This is the best explanation from all the sites i have seen.The thin is your explanation is Understandable even to a begineer.

    ReplyDelete
  2. i want date seeter and getter formates

    ReplyDelete

Select Menu