Java Reference
In-Depth Information
public class ErrorCodes {
// Define a static structure that contains all the I/O error codes.
static int[] IOCodes = {1, 2, 3, 10, 12, 22, 23, 30};
// A method that returns all the error codes for I/O functions:
public int[] errorCodesIO {
// Create an array to hold the error codes.
int[] results = new int[IOCodes.length];
// Copy the array (actually the method System.arraycopy() would be a
// better choice than this loop).
for(int x = 0; x < IOCodes.length; x++) {
results[x] = IOCodes[x];
}
// Return the array that you have just loaded.
return results;
}
Before I move on to the next topic, let's visit this statement again.
ErrorMsg[] myErrorMsgs;
What does
myErrorMsgs[0]
contain after this statement?
ErrorMsg[] myErrorMsgs;
myErrorMsgs = getErrorMsgs();
if (someCondition)
myErrorMsgs = getExtendedErrorMsgs();
Since the array has not been initialized via the
new
operator, it does not contain any
ErrorMsgs
. However, I did say it
will
contain reference variables of this object type.
After this statement,
myErrorMsgs = new ErrorMsg[10];
all the elements in
myErrorMsgs
will contain empty object reference variables, or
null
. Therefore, this statement:
if (myErrorMsgs[0] == null)
will always evaluate to
true
until
myErrorMsgs[0]
is assigned to an actual reference
variable.