The advantage of a getters is you can later add code to decompress, cache or modify the value, without changing any of the client code. Further, getters can mask the fact the retrieved value is virtual, calculated from other fields.
Eiffel lets you write getters even when the client code is tersely written as if the values were instance variables. However, Java insists on fluffy syntax both where the getter is defined and invoked.
Modern IDEs (Integrated Development Environments) such as IntelliJ Idea will compose a set of getters and setters for you, given just the private instance variables.
Normally within a class you don’t usually use the getters; you go direct to the underlying instance variables, though sometimes it makes sense from a maintenance point of view to channel through getters. Within a package or from outside, you would nearly always go through getters rather than going to directly to public instance variables.
You can also write getters for static classes.
|
|
available on the web at: |
http://mindprod.com/jgloss/getter.html |
optional Replicator mirror
|
J:\mindprod\jgloss\getter.html | |
![]() |
Please email your
feedback for publication,
letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear
wording, broken/redirected link reports, suggestions to improve this page or comments to
Roedy Green :
| |
| Blog | Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[50.19.155.235] |
|
| Feedback | You are visitor number 19,997. | |