•
Set appropriate access.
•
Avoid creating interfaces.
•
Use static inner classes.
•
Avoid unnecessary field initialization.
•
Import individual classes.
Setting appropriate access
When you create code libraries, you can significantly reduce the size of your compiled code by using the appropriate access
modifiers for fields and methods. Declare fields as private whenever possible. In addition to being good coding practice, this
allows the compiler to optimize the .cod file. When possible, use the default (
package
) access instead of public access (that
is, omit the
public
and
protected
keywords).
Avoiding creating interfaces
When you create API libraries, avoid creating interfaces unless you foresee multiple implementations of the API. Interfaces
produce larger, slower code.
Using static inner classes
When you use an inner class to hide one class inside another, but the inner class does not reference the outer class object, declare
the inner class as static. This action prevents the creation of a reference to the outer class.
If you use an inner class for name scoping, make it static.
Code sample
class outer {
static class inner {
...
}
}
Avoiding unnecessary field initialization
Where possible, allow fields to initialize automatically as follows:
•
object references are initialized to null
•
int
,
byte
, or
long
is initialized to 0
•
Boolean is initialized to false
You must explicitly initialize local variables in a method.
Fundamentals Guide
Best practices for writing an efficient BlackBerry Java Application
19