Why does java.awt.Dimension have public variables?

Isn't it true that a class with public variables is considered weak in encapsulation and this is not a bad design practice?

If so, why does java.awt.Dimension have 2 public variables width and height ?

+7
java awt
source share
3 answers

I think that exposing public fields is not a violation of encapsulation itself. Encapsulation is a property of entities that hide a complex internal structure within themselves. This complex structure cannot be accessed directly and therefore not at risk of being broken. Access to it is carried out only through publicly available methods that do their job well and do not damage the complex internal structure. For example, we do not have access to the HashMap hashtable directly, and we cannot spoil it. We use only the get and put methods, which correctly take care of the state of the hash table. But where is the complex structure of the Dimension object? What things will break when we just write d.width = 23 ?

I think there are reasons to use public getters / setters instead of public fields in this case: taste and conventions. The latter is perhaps more significant. But it also depends on where and why we apply these agreements.

+3
source share

If you look at the JavaDoc for measurement: http://docs.oracle.com/javase/7/docs/api/java/awt/Dimension.html

You will see that the public fields have been there since 1.0 (1996). The language has evolved in order to use over time many principles and best practices to become the language that it is today. This included a rework of the AWT model for version 1.1, from which accessors and mutators were added for these fields.

Here are some archived releases for the JDK 1.1 release: http://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/guide/awt/HowToUpgrade.html

From what it is indicated:

"These changes allow us to use programs such as GUI builders and JavaBeans - the use of programs to query components to determine the properties of components."

+1
source share

The measurement has been around for a very long time, and in the early days of Java there was a real performance issue. So they used publicly available variables and made it mutable - two main mistakes to get a few beats.

Everything that you guys ask questions about premature optimization, you can learn a good lesson here. (Added)

The sizes change a lot (when the window is resized), and by changing it, you “save” to select new objects if necessary - just reuse the old one. Which, in the early days of Java, made some sense. In addition, they gained access to many, because the graphical interface drew nested buttons inside the components inside the frames, therefore, to save several cycles when redrawing, do not use the function call. Again, it makes little sense.

The problem is that currently with all multi-core processors and multi-threaded code, you often create protective copies of Dimension. Having lost all this "adrenaline", plus, the source of errors is annoying.

0
source share

All Articles