Java Reference
In-Depth Information
private String hidn;
String pkgstr;
protected String protstr;
public String wideOpen;
3.4.2.2
Another keyword qualifier on declarations that we need to describe is the
static keyword. When a variable is declared static then there is only one
instance of that variable shared among all instances of the class. Since the vari-
able exists apart from a particular instance of the class, one refers to it with the
class name followed by a dot followed by the variable name, as in System.out .
Similarly, methods can be declared static as well. This also means that
you don't need an instance of the class to call them, just the class name, as in
System.getProperties() .
Now with Java 5.0, you don't even need the class name, provided that you
have a static import statement at the top of your class, for example:
The static statement
import static java.lang.System.*;
3.4.2.3
Another way that static is often seen is in conjunction with the final key-
word. When a variable is declared final then a value can be assigned to it once,
but never changed. This can make for good constants.
Since public will make the variable visible to all other classes, static
will make it a class variable (available without an instance of that class), and
final will keep it from being altered (even though it is publicly available), then
combining all of those gives us a good declaration for a constant, for example:
The final statement
public static void long lightSpeed = 186000; // mps
New to Java 5.0 is the explicit creation of enumerated types. Prior to 5.0,
programmers would often use static final constants even when the partic-
ular value was unimportant, as a way to provide compile-time checking of the
use of the constant values. Here is an example of a declaration of a set of
enumerated values:
enum WallMods { DOOR, WINDOW, VENT, GLASSBLOCK };
Search WWH ::




Custom Search