posted 14 years ago
We use getters and setters because it allows us to do more than just get/set. For example we could do some processing, inform listeners, update back-end data or other local data that relies on the contents of the value being set. It gives the ability to protect the internal storage of the value by making copies when needed and appropriate, and synchronize when an object may be used by multiple threads.
You can add these features as needed without changing client code (the code that uses the DTO) if you use methods. If you allow direct access to the fields and you need to enforce synchronization or inform listeners we either have to rely on client code to synchronize/inform (not safe), or change the interface to our class to use set/get methods, thus breaking client code.