Home
Code style
The Java code style preferences allow you to configure naming conventions, style rules, and comment settings.
To demonstrate setting up a style, we define a sample style in which the following conventions are defined:
Member attributes or fields will be prefixed by an m.
Static attributes or fields will be prefixed by an s.
Parameters of methods will be prefixed by a p.
Local variables can have any name.
Boolean getter methods will have a prefix of is. To configure the customized style, do these steps:
Select Windows Æ Preferences Æ Java Æ Code Style .
Select the Fields row and click Edit.
In the Field Name Conventions dialog, enter m in the Prefix list field and click OK.
Repeat the same steps for all the foregoing conventions as defined.
Figure 3-22 Code style preferences
These coding conventions are used in the generation of setters and getters for Java classes:
Whenever a prefix of m followed by a capital letter is found on an attribute, this would ignore the prefix and generate a getter and setter without the prefix.
If the prefix is not found followed by a capitalized letter, then the setter and getter would be generated with the first letter capitalized followed by the rest of the name of the attribute. An example of the outcome of performing code generation of a getter is shown in Example | 1 for some common examples of attributes.
Note: The capitalization of getters in Application Developer is based on the way the attributes are named.
Example 3-1 Example snippet of code generation output for getters
private long mCounter;private String maddress;private float m_salary;private int zipcode;/*** @return the m_salary*/public float getM_salary() {return m_salary;}/*** @return the maddress*/public String getMaddress() {return maddress;}/*** @return the counter*/public long getCounter() {return mCounter;}/*** @return the zipcode*/public int getZipcode() {return zipcode;}
The settings described in Table | 5 specify how newly generated code should look. These settings are configured in the Code Style preferences page .
Table 3-5
Action Description Qualify all generated field accesses with 'this.'
If selected, field accesses are always prefixed with this., regardless whether the name of the field is unique in the scope of the field access or not.
Use 'is' prefix for getters that return boolean
If selected, the names of getter methods of boolean type are prefixed with is rather than get.
Automatically add comments for new methods and types
If selected, newly generated methods and types are automatically generated with comments where appropriate.
Add '@Override' annotation for overriding methods
If selected, methods that override an already implemented method are annotated with an @Override annotation.
Exception variable name in catch blocks
Specify the name of the exception variable declared in catch blocks.
Description of code style settings
ibm.com/redbooks