Java Getters & Setters

getter and setter in java

Java Getters and Setters are used to effectively protect your data, particularly when creating classes. For each variable, the get method returns its value, while the set method sets the value.

Getters:

Getters start with get, followed by the variable name, with the first letter of the variable name capitalized.


Setters:

Setters start with set, followed by the variable name, with the first letter of the variable name capitalized.

Example:

  public class Vehicle {
  private String color;
  // Getter
  public String getColor() {
    return color;
  }
 // Setter
  public void setColor(String c) {
    this.color = c;
  }
}

The getter method returns the value of the attribute.

The setter method takes a parameter and assigns it to the attribute.


Getters & Setters:

Once our getter and setter have been defined, we can use it in our main:

Example:

 public static void main(String[ ] args) {
  Vehicle v1 = new Vehicle();
  v1.setColor(“Red”);
  System.out.println(v1.getColor());
}
//Outputs “Red”

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *