+ 1
What is this in the following program in setter block....?
public class Vehicle { private String color; // Getter public String getColor() { return color; } // Setter public void setColor(String c) { this.color = c; } } class Program { public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); v1.setColor("Red"); System.out.println(v1.getColor()); } }
1 Antwort
+ 2
this is a reference to the object itself. You only need to use this if the class variable is the same as the setter parameter, like so:
public void setColor(String color) {
this.color = color;
}
this differentiates between the class variable and the method argument