getter : Java Glossary

getter
An instance method that methods retrieve a value from an object instance or static class, e.
Getters are just a naming convention. Methods that behave like public variables begin with the lower-case letters get followed by the camel-case name of the internal associated private, possibly virtual, variable, e.g. getWidth(). Getters that return a boolean begin with the lower-case letters is e.g. isVisible();

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 IDE (Integrated Development Environment)s 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.

Improved Syntax

Getter and setter syntax is ugly. They were a cheap kludge to the Java language, that could be implemented with no effort. Other languages such as Eiffel, Bali and Delphi have a much more elegant way of handling getters, called properties. To the client programs, getters look like ordinary public variables. That makes it easy to change the implementation back and forth between a simple efficient public variable and a method. This makes programs much more readable e.g.
// property syntax
x.a = x.b + x.c;
// vs
// getter syntax
x.setA ( x.get( a ) + x.getb( b ) );

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/getter.html J:\mindprod\jgloss\getter.html
logo
Please email your , 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 : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.214]
You are visitor number 17,879.