I was looking into an open-source game framework project written in Java. It has several classes that:
- Have public instance variables.
- Have getter/setters for such variables.
Generally, I write getters/setters when I want to encapsulate some behavior that occurs when a property is modified.
However, looking at the code, it literally goes something like this:
public class Test {
public int value;
public void setValue(x) {
value = x;
}
public void getValue() {
return value;
}
}
Then I thought about checking out the rest of the framework and see how do they modify such value, which may give me a hint on why did they do the above.
From what I've seen, they do it like this:
object.value = 100;
Without using the getters/setters.
So now I'm not sure what was the point of creating getters/setters.
So, my question is, is there a reason I would want to create getters/setters for a public value, if such getters/setters don't even implement any kind of special behaviour?