- 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 .
- Method name should follow naming convention setVARIABLENAME().
- It should accept some value as an argument. here method argument should be of type of variable.
- It should have a statement to assign argument value to corresponding variable.
- It does not have any return type. void should be the method return type.
- In order to set some value to variable we need to call corresponding setter method by passing required value.
- package settterandgettermethods;
- public class SetAndGet {
- private String name;
- private int id;
- public void setName(String name) {
- this.name = name;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
Get method / Getter method in java:
- Purpose of Getter method is to get the value of the instance variable.
- Method name should follow naming convention getVARIABLENAME().
- It should not have any arguments.
- It should return corresponding variable value.
- So return type must be of type of variable we are returning from the method.
- In order to get the variable value we need to call corresponding getter method of variable.
- package settterandgettermethods;
- public class SetAndGet {
- private String name;
- private int id;
- public String getName() {
- return name;
- }
- public void setId(int id) {
- this.id = id;
- }
- public static void main(String args[]){
- SetAndGet obj = new SetAndGet();
- String name =obj.getName();
- }
- }
Java program to get and set variable values without setter and getter methods
- package settterandgettermethods;
- public class SetAndGet {
- private String name;
- private int id;
- public static void main(String args[]){
- SetAndGet obj = new SetAndGet();
- obj.name="setting some value";
- obj.id=1;
- System.out.println(obj.name);
- System.out.println(obj.id);
- }
- }
Output:
- setting some value
- 1
Java program to get and set variable values with setter and getter methods
- package settterandgettermethods;
- public class SetAndGet {
- private String name;
- private int id;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public static void main(String args[]){
- SetAndGet obj = new SetAndGet();
- obj.setName("java");
- obj.setId(1);
- System.out.println(obj.getName());
- System.out.println(obj.getId());
- }
- }
Output:
- java
- 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.
ReplyDeletei want date seeter and getter formates
ReplyDeletethanks a lot 🤍
ReplyDelete