Getters & Setters 

Getters & Setters

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 start with get, followed by the variable name, with the first letter of the variable name capitalized.
Setters start with set, followed by the variable name, with the first letter of the variable name capitalized.

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.

The keyword this is used to refer to the current object. Basically, this.color is the color attribute of the current object.


Comments

Post a Comment

Popular posts from this blog

Exp : 1 & 2 : Working with HTML and CSS

DOS Attack code for python

DV and LS Routing Algorithm Implementation in ns2