Persistent storage on the BlackBerry Wireless Handheld is limited. The following programming tips should minimize the size of your compiled code (.cod files) and maximize program speed. These tips are also in the Developer Guide that is included with the BlackBerry JDE.Set Appropriate Access
When you create code libraries, you can significantly reduce the size of your compiled code if you use the appropriate access modifiers for fields and methods. The following steps help reduce the size of compiled code:
- Declare fields private whenever possible. This is good coding practice and enables 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).
Use Shorthand for Evaluating Boolean Conditions
Use this efficient shorthand for evaluating Boolean conditions. Instead of writing the code as in example 1, you could write the code as in example 2:
// Example 1 - Avoid this
if(something_that_evaluates_to_true) {
return true;
} else {
return false;
}
// Example 2 - Do this
return(something_that_evaulates_to_true);
The resulting compiled code is shorter.
Avoid Creating Interfaces
When creating API libraries, avoid creating interfaces unless you foresee multiple implementations of the API. Interfaces produce longer, slower code.
Use Static Inner Classes
When you use an inner class to hide one class inside another, but you do not need the inner class to reference the outer class object, declare the inner class static. This suppresses the creation of the reference to the outer class. For example, the following code requires a reference to the outer class object:
class outer {
int i;
class inner {
inner() {}
int example() {return i;}
}
}
In contrast, this code only scopes the name of the inner class:
class outer {
static class inner {
}
}
is a shorter version of this code:
class outer {
}
class outer$inner {
}
The only time you should use a non-static inner class is when you need access to data
in the outer class from within methods of the inner class. If you are using an inner
class only for name scoping, make the inner class static.
Make Classes Final
When creating code libraries, ensure that you mark a class as final if you know it will never be extended. The presence of the final keyword enables the compiler to generate faster code.
Use int Instead of Long
In Java, a long is a 64-bit integer. Since the BlackBerry Wireless Handheld uses a 32-bit processor, operations run two to four times faster if you use an int instead of a long.
Using Static Strings
When defining static fields (also called class fields) of type String, you can increase program speed by using static variables (not final) instead of constants (final). The opposite is true for primitive data types, such as int.
For example, you might create a String object:
private static final String x = "example";
For this static constant (denoted by the final keyword), a temporary String instance is created every time you use the constant. The compiler eliminates x and replaces it with the string “example” in the bytecode, so that the virtual machine (VM) has to perform a hash table lookup each time you reference x.
In contrast, for a static variable (no final keyword), the String is created once. The VM performs the hash table lookup only when it initializes x, which makes access faster.
Note: The BlackBerry Java Development Environment (JDE) compiler (rapc) automatically marks any classes as final that are not extended in an application .cod file.
Note: It is acceptable to have public constants (that is, final fields), but you should always make variables private.