Getters and Setters and this. keyword

2.3k views Asked by At

I'm new to coding and I'm confused about getters and setters in Java. I know that getters and setters are used for encapsulation. But if you have a constructor that creates a person of a specific gender and length. Should both of these characteristics have a setter and a getter?

public Person(Gender gender, Length length) {
this.gender = gender;
this.length = length;
}

Does the this.gender serve as a setter? If no, what's its function?

Do I need to make a getter and setter for these? In code examples i've found they only have a getter, but not a setter. But i don't really understand why. Thanks in advance!

3

There are 3 answers

5
Idos On BEST ANSWER

In code examples I've found they only have a getter, but not a setter. But I don't really understand why.

The functionality you describe is so when a Person object is created, nobody can set its gender and length. If a user wishes, he/she can create a new Person (new Person(...)) with attributes as they wish. But after a Person is created, you cannot set the attributes.

But does the this.gender = gender work as a setter or not? I can't seem to find out what its function is.

It does work as a setter (though not a setter by itself since it's not a function). But only within the constructor. As I said above.

Note that if the gender and length fields of a Person are not private then they can potentially be set/get outside of a set/get methods.

0
ffguven On

Getter and setter methods in object oriented programming is for controlling the client not to change your variables in an unexpected behavior.

public Class Person{
  private int gender;
  private int length;

  public Person(int gender,int length){
    this.gender = gender;
    this.length = length;
  }

  public int getGender(){
     return this.gender;
  }
  public void setGender(int setGender){
     if(setGender==1 || setGender == 0){
       this.gender = setGender;
     }else{
       this.gender = -1;
     }
  }
  public int getLength(){
     return this.length;
  }
  public void setLength(int setLength){
     if(setLength>100){
       this.length = setLength;
     }else{
       this.length = -1;
     }
  }
}

like in this code sample, if you have some constraints about your variables and you need to dictate client to update these variables in your way, you should use getter and setter methods in order to control your variables updating way.

In this example, if the client's gender is different from 0 or 1 (if it enters invalid gender code) we are assigning -1 to its variable in order to see that invalid gender, length logic is also same.

0
Manoj Kumar On

The constructor example code you have given is used to set/initialize the variables of Person class when you create Person object by invoking the constructor method. Internally, the attribute values get set by this. So, you can say it's functionality is like setter. After this you only need getters to fetch and use the value held by these attributes.

However, you can also write specific setter method for each attribute to set the values individually for each of them on the same Person object explicitly.

Regards

Manoj