Java Reference
In-Depth Information
The applet then implements all of the methods defined by the MouseListener and
MouseMotionListener interfaces. These are the event handlers for the various mouse
events. Each method handles its event and then returns.
More Java Keywords
Before concluding this chapter, a few more Java keywords need to be briefly discussed:
transient
volatile
instanceof
native
strictfp
assert
These keywords are most often used in programs more advanced than those found in this
book. However, an overview of each is presented so that you will know their purpose.
The transient and volatile Modifiers
The transient and volatile keywords are type modifiers that handle somewhat specialized
situations. When an instance variable is declared as transient , then its value need not per-
sist when an object is stored. Thus, a transient field is one that does not affect the persisted
state of an object.
The volatile modifier tells the compiler that a variable can be changed unexpectedly by
other parts of your program. One of these situations involves multithreaded programs. In a
multithreaded program, sometimes two or more threads will share the same variable. For
efficiency considerations, each thread can keep its own, private copy of such a shared vari-
able, possibly in a register of the CPU. The real (or master ) copy of the variable is updated
at various times, such as when a synchronized method is entered. While this approach
works fine, there may be times when it is inappropriate. In some cases, all that really mat-
ters is that the master copy of a variable always reflects the current state, and that this cur-
rent state is used by all threads. To ensure this, declare the variable as volatile .
instanceof
Sometimes it is useful to know the type of an object during run time. For example, you
might have one thread of execution that generates various types of objects and another
thread that processes these objects. In this situation, it might be useful for the processing
Search WWH ::




Custom Search