Game Development Reference
In-Depth Information
Java's Static Modifier: Variables or Methods That Exist
in a Class (Not in Objects)
As you have already seen, the static keyword can be used in conjunction with the final
keyword to create a constant. The static keyword is used to create Java constructs
(methods or variables) that exist independently, or outside, any object instances that are
created using the class that static variables or static methods are defined in. A static
variable in a class will force all instances of the class to share the data in that variable,
almost as if it is a global variable as far as objects created from that class are con-
cerned. Similarly, a static method will also exist outside instanced objects for that class
and will be shared by all those objects. A static method will not reference variables
outside itself, such as an instanced object's variables.
Generally, a static method will have its own internal (local or static) variables and
constants and will also take in variables, using the method parameter list, and then
provide processing and computation, based on those parameters and its own internal
(static local) constants if needed. Because static is a concept that applies to instances
of a class, and is thus at a lower level than any class itself, a class would not be de-
clared using a static modifier keyword.
Java'sAbstractModifier:ClassesandMethodstoBeEx-
tended and Implemented
The Java abstract modifier keyword has more to do with protecting your actual code
than with code that has been placed in memory (object instances and variables, and so
on) at runtime. The abstract keyword allows you to specify how the code will be used
as a superclass, that is, how it is implemented in a subclass once it is extended. For this
reason, it applies only to classes and methods and not to data fields (variables and con-
stants).
A class that has been declared using the abstract modifier keyword cannot be in-
stanced, and it is intended to be used as a superclass (blueprint) to create ( e x tend ) other
classes. Because a final class cannot be extended, you will not use the final and abstract
modifier keywords together at the class level. If a class contains any methods that have
been declared using the abstract modifier keyword, the class must itself be declared an
abstract class. An abstract class does not have to contain any abstract methods,
however.
Search WWH ::




Custom Search