Re: Beginner doubt about access and methods
Tom Hawtin wrote:
The reason for making variables private is to encourage encapsulation.
If you then add get and set methods, you've destroyed most of the reason
for making the variable private. Prefer operations that make sense to
the interface, rather than exposing the implementation.
You've destroyed part of the reason, perhaps, but not most of it.
get and set methods are a standard idiom recognized by a bunch of frameworks,
such as JSF and JSTL, that make it possible for them to use Java classes
without developer configuration.
get and set methods hide the implementation of attributes while exposing the
attributes. They allow tricks like not even having a private variable for the
attribute.
get and set methods can be made final, preventing abuse of the instance
variable that might be possible by increasing its visibility.
get and set hide the implementation of an attribute rather than exposing it.
I am not saying that use of these methods is a be-all and end-all, but these
are motivations for the idiom.
--
Lew