If you have code like this:
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public Rectangle getBounds() {
return bounds;
}
Then all you’ve done is add boilerplate code that does nothing.
If you’ve got code like this:
public void setBounds(Rectangle bounds) {
this.bounds.set(bounds);
}
public Rectangle getBounds() {
return new Rectangle(bounds);
}
Then fair enough, you’ve protected your internal state. You should always try to do this.
However you’ve exposed to the outside that the object has a bounds. The object oriented way would be to have methods on the object that uses the bounds instead of exposing it. So instead of:
public void mousePressed(MouseEvent e) {
if (object.getBounds().contains(e.getPoint())) {
object.activate();
}
}
You could do:
public void mousePressed(MouseEvent e) {
object.activateIfPointIsInside(e.getPoint());
}
Now in general, things are not black or white. You don’t always use one method over another. But knowing about different ways of doing things can help you write better code.