Better understanding of getters and setters - Java
Hi. I'm still confused as to how getters and setters work. I understand how paramaters and arguments work but not exactly this concept. From my understanding, getters are supposed to get a variable within the class? But what's confusing is when I think of 'getting something' I think of getting it and keeping it, however the getter method only has single statement 'return color' so then the other question is where is it returning it to? Back to the orginal private protected variable? Also the setter method.. I understand it uses parameters but where are the arguments coming from? 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()); } } Also, it seems a bit long to have to use two different methods, why not just use one method that gets and sets rather than creating two? One last thing, is it possible you could provide a short simple real world example of what would happen if we don't use getters and setters to protect the variable?