So do I. You've just described exactly why indiscriminate use of
accessors and mutators is a bad idea. Inflating the interface
unnecessarily leads to maintenance and enhancement problems.
To restate the point, what I was saying is that it's a bad idea to
simply write an accessor (and possibly a mutator) for each and every
instance variable. Such indiscriminate use is indeed a bad idea. But I
didn't intend the statement to include other, possibly indiscriminate,
uses. IOW, it wasn't directed against the use of accessors and mutators
itself.
Objects should tell each other what to do, not ask each other for
state.
You seem to ignore the fact than an Object can purposefully be a holder
of a state. As such, its state would be its property. And as soon as
that state become in the slightest way dynamic, that Object will also be
a "doer". It amounts to the exact same thing whether you "tell" an
object to report its state to you or whether you "ask" it for its state.
As for the strictly imperative approach you could be interpreted as
advocating, it may be an interesting paradigm, but one thing is for sure
and that's that it hasn't its place in the Java language.
I didn't advocate against accessors and mutators, but against a very
specific /spirit/ in which they could be applied. I would still maintain
that, as a rule, a class should *never* expose instance variables. As a
valid exception to this rule, such a highly specific and "leafy" (in the
class tree) class as GridBagConstraints comes to mind.
Everything else which describes a property of the Object, regardless of
whether it is implemented via a single variable or not, should be
accessed through methods, not directly. It is true, as you say, that
this inflates the interface. I disagree that this is unnecessary. I
disagree that it leads to maintainance problems. I maintain that it is
_vital_ for enhancement.
The OP has later refined his question to uses of accessors and mutators
methods of an Object /within/ the Object's class. But even there these
methods make a great deal of sense: for polymorphism (I am, by the way,
strictly opposed to the "classes and variables should be final by
default" idiom). What's more, this kind of strict separation between the
code in a class and the one outside of it is wrong, IMO. The separation,
again, should be a semantical one. It's the design that matters here.
And as design goes, encapsulation is almost a categorical imperative
(there is of course no such thing as Cant's categorical imperative, but
almost). It is well appropriate to define different levels of
encapsulation within a class itself, levels which you can later extract
and put in their own class as the codebase grows. Again: ease of
maintainability. Considering code in a chorogically dynamic perspective,
there is no such thing as a strict boundary between classes.
df.