Yes, it is "safe". Quote from Java Performance Tuning:
[...] This same Dimension object can be reused for multiple components. [...]
Depends on what you want though. Note that the implementation in Component
does not copy the content of the argument, but stores the reference:
public void setPreferredSize(Dimension preferredSize) {
...
this.prefSize = preferredSize;
...
}
so changing d
will affect the dimension object stored in both tf1
and tf2
.
(What I'm saying is that the code below may not do what you would expect.)
Dimension d = new Dimension(80, 20);
tf1.setPreferredSize(d);
d.width += 1; // <-- will affect also tf1.
tf2.setPreferredSize(d);