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()); } }

17th Nov 2018, 8:01 PM
triny
1 Réponse
+ 2
it's necessary if you use the same names. for example if you write in setColor string color, you must use "this"
17th Nov 2018, 8:20 PM
_yaroslavv [online_everyday]
_yaroslavv [online_everyday] - avatar