Code improvements
We want to discuss in the following pages a couple of optimizations related to particular coding situations and common patterns. These tips are examples of how common habits in practical everyday development work may lead to performance faults.
Getters and setters
One of the core concepts used in object-oriented programming is encapsulation; as you know, it means that the fields of an object should not be accessed directly by other objects. So, you can encapsulate an object's fields in Java by using the private modifier and by creating getter and setter methods to let other objects access them. This guarantees that the class itself has complete control over its own fields and no one else can use it. Then, you are free to create read-only or write-only fields, simply defining just the related method and avoiding defining the other one.
The benefits of encapsulation are not at issue, but they come with a cost. Accessing fields directly is three times faster than using a getter...