img
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Here is the output of this program:
a = 42
b = 99
Introducing final
A variable can be declared as final. Doing so prevents its contents from being modified.
This means that you must initialize a final variable when it is declared. For example:
final
int
FILE_NEW = 1;
final
int
FILE_OPEN = 2;
final
int
FILE_SAVE = 3;
final
int
FILE_SAVEAS = 4;
final
int
FILE_QUIT = 5;
Subsequent parts of your program can now use FILE_OPEN, etc., as if they were constants,
without fear that a value has been changed.
It is a common coding convention to choose all uppercase identifiers for final variables.
Variables declared as final do not occupy memory on a per-instance basis. Thus, a final
variable is essentially a constant.
The keyword final can also be applied to methods, but its meaning is substantially
different than when it is applied to variables. This second usage of final is described in the
next chapter, when inheritance is described.
Arrays Revisited
Arrays were introduced earlier in this topic, before classes had been discussed. Now that you
know about classes, an important point can be made about arrays: they are implemented as
objects. Because of this, there is a special array attribute that you will want to take advantage of.
Specifically, the size of an array--that is, the number of elements that an array can hold--is
found in its length instance variable. All arrays have this variable, and it will always hold the
size of the array. Here is a program that demonstrates this property:
// This program demonstrates the length array member.
class Length {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
int a3[] = {4, 3, 2, 1};
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home