Eclipse generate getter setter tip to deal with field prefixes suffixes

Here is a nifty feature I found in Eclipse regarding generating getters and setters. Normally the names Eclipse assigns to the getter and setter methods are the field names as-is suffixed to get and set. For e.g. if you have a field called “lastName” – the getter would be getLastName() and the setter would be setLastName(). Now some people follow coding conventions by prefixing fields with a certain letter to indicate they are class level variables.  e.g. mLastName, mFirstName etc.  In such a case when Eclipse is used to generate getters/setters, then the method names become getmLastName(), setmLastName() etc.  Those names are nothing short of disgusting:

Getter Setter method name with field prefix
Getter/setter name has field prefix

Fortunately this can be easily fixed.  You can tell Eclipse that you prefix/suffix your fields in a certain way and the prefix/suffix will automatically be removed from the generated getters and setters. Click on Window -> Preferences -> Java -> Code Style.  Select Fields and click on Edit to specify the field name conventions.

Set field name conventions
Set field name convention

Now, things are looking good.

Getter/Setter without field prefix
Getter/Setter without field prefix

Eclipse, what would life be without it ?

Leave a Reply

Your email address will not be published. Required fields are marked *