OOP question....proper way to have 2 objects reference each
kplug at davidb.org
Tue Nov 24 16:45:18 PST 2009
On Wed, Nov 25, 2009 at 12:34:10AM +0000, SJS wrote:
>> 2. Insisting on declaring classes public, private or protected....as if I
>> need to be protected from myself because I might be moronic enough to
>> "accidentally" call a method I shouldn't.
>(b) It gives the compiler some information about what can be optimized.
This isn't a subtle thing, BTW. The JVM will often generate _better_
code for a getter/setter of a private field than it would if the field
itself is public.
The strongest example is if the field is set only rarely. If there is
only one setter, and the field is private, the JVM can inline the
_value_ of the field. The getter will be annotated to invalidate the
code that has inlined that particular value. It's basically making
the set much more expensive at the cost of completely eliminating an
indirection on the getter.
Aside from all of this, it just helps to make the abstractions
meaningful. Programmers often try using API calls, sometimes just
trying to make something work, and if you expose your internals, their
usage will become fragile.
More information about the KPLUG-List