0
Is "this" necessary within getter and setter?
Why does the following code work, even if I don't use the keyword "this" to refer to the current object? public class Vehicle { private String color; // Getter public String getColor() { return color; } // Setter public void setColor(String c) { 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
it's necessary if you use the same names. for example if you write in setColor string color, you must use "this"