Prev | Current Page 142 | Next

Carl Reynolds and Paul Tymann

"Schaum's Outline of Principles of Computer Science"

If you read the Javadoc for the
class StreamTokenizer, for example, you will see a set of four class constants that are declared static.
These constants are TT_EOF, TT_EOL, TT_NUMBER, and TT_WORD. These constants represent the possible
values (the token types) that a StreamTokenizer will return when it extracts a token (a String,
a number, or an EndOfFile/EndOfLine flag) from an InputStream. Another example from Java is the class
Color, which defines a long list of static constants used to represent commonly used colors.
Aside from using static variables for class constants, it is generally wise to avoid static variables unless you
have a special reason to use them (as, for example, a need to keep a count of all the objects created). The reason
is that, typically, many objects can modify a static variable, and over time the probability of some new class
taking such liberties will grow. Discovering the cause of unexpected behavior related to static variables can be
difficult.
Static methods likewise are associated with a class as a whole, and are accessible from any object simply
by referencing the class name that provides the static method. For instance, the Java class Integer has a set
of static methods related to integer numbers.


Pages:
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
śmieszne dowcipy dieta light życzenia ślubne portal Connie Talbot