3 ms·
TL;DR: Fields are fields, methods are methods, auto-generating getters/setters blurs the line in unhelpful ways. That's not really true in Java. The getter/set
by nl 14y ago
TL;DR: Fields are fields, methods are methods, auto-generating getters/setters blurs the line in unhelpful ways.
That's not really true in Java. The getter/setter pattern is baked into a lot of the standard Java libraries.
For example, Javabeans are:
a Java Object that is serializable, has a 0-argument constructor, and allows access to properties using getter and setter methods.[1]
Things like JPA, Jersey, Guice etc all rely on the Javabeans convention.
Yes, Java should have real properties like Object Pascal & C# does, but it doesn't, and getters & setters are harmless to anything except aesthetic sensibilities.
TL;DR: Getters & setters are a useful convention that allow many things that are not possible with public fields.
[1] http://en.wikipedia.org/wiki/JavaBeans http://en.wikipedia.org/wiki/JavaBeans