getter setter java code example

Example 1: get und set methoden java

this.getLukas();

public int getLukas() { return this.lukas }

Example 2: java setter

public setValue(value) {
  this.value = value;
}

Example 3: setter&getter java

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

Example 4: java getter

public getValue() {
  return value;
}

Example 5: getter setter

public class Person {
  private String name; // private = restricted access

  // Getter
  public String getName() {
    return name;
  }

  // Setter
  public void setName(String newName) {
    this.name = newName;

Example 6: setter&getter java

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

// Outputs "Red"

Tags: