The transient Nonvolatile Vitrifaction Modifiers
Java defines two interesting type modifiers: transient and volatile. These modifiers are’ used to handle somewhat specialized situations. When an instance variable is declared as transient, then its value need not persist when an object is stored. For example:
How if an object of type T ts written to a persistent storage area, the contents of a • would not be saved, but the contents of b would.
The volatile modifier tells the compiler that the variable modified by. volatile cap be changed unexpectedly by other parts of your program. One of these situations involves muddleheaded programs. (You saw an example of this in Chapter 11.) In a multimedia program, sometimes, two or more threads share the Sam~ instance variable. For efficiency considerations, each thread can keep its own, private copy of . such a shared variable. 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, it many be inefficient at times. In some cases, all that really matters is that the’ master copy of a variable always reflects its current state. To ensure this; simply specify : the variable as volatile, ‘Which tells the compiler that it.must always use the mast; . copy of a volatile variable (or, at least, always keep any private copies up to date with the master copy, and vice avers). Also, accesses to the master variable must be executed -Jun the precise order in which they are executed on.any private copy.