What is this in the following program in setter block....? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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()); } }

24th Dec 2016, 7:40 AM
Saurabh Pandey
Saurabh Pandey - avatar
1 Answer
+ 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
25th Dec 2016, 6:18 AM
Gabe Rust
Gabe Rust - avatar