getter : Java Glossary


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 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.

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 ) );

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number