Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
6114_LEWI_AppE_685-688.qxd 2/1/08 8:19 AM Page 685 Java Modifiers E This appendix summarizes the modifiers that give particular characteristics to Java classes, interfaces, methods, and variables. For discussion purposes, the set of all Java modifiers is divided into two groups: visibility modifiers and all others. Java Visibility Modifiers The table in Figure E.1 describes the effect of Java visibility modifiers on various constructs. Some relationships are not applicable (N/A). For instance, a class cannot be declared with protected visibility. Note that each visibility modifier operates in the same way on classes and interfaces and in the same way on methods and variables. Default visibility means that no visibility modifier was explicitly used. Default visibility is sometimes called package visibility, but you cannot use the reserved word package as a modifier. Classes and interfaces can have default or public visibility; this visibility determines whether a class or interface can be referenced outside of its package. Only an inner class can have private visibility, in which case only the enclosing class may access it. Modifier default (no modifier) Classes and interfaces Methods and variables Visible in its package. Visible to any class in the same package as its class. public Visible anywhere. Visible anywhere. protected N/A Visible by any class in the same package as its class. private Visible to the enclosing class only Not visible by any other class. FIG URE E .1 Java visibility modifiers 685 6114_LEWI_AppE_685-688.qxd 686 A PPEND IX E 2/1/08 8:19 AM Page 686 Java Modifiers A Visibility Example Consider the situation depicted in the Figure E.2. Class P is the parent class that is used to derive child classes C1 and C2. Class C1 is in the same package as P, but C2 is not. Class P contains four methods, each with different visibility modifiers. One object has been instantiated from each of these classes. The public method a() has been inherited by C1 and C2, and any code with access to object x can invoke x.a(). The private method d() is not visible to C1 or C2, so objects y and z have no such method available to them. Furthermore, d() is fully encapsulated and can only be invoked from within object x. The protected method b() is visible in both C1 and C2. A method in y could invoke x.b(), but a method in z could not. Furthermore, an object of any class in package One could invoke x.b(), even those that are not related to class P by inheritance, such as an object created from class Another1. Method c() has default visibility, since no visibility modifier was used to declare it. Therefore object y can refer to the method c() as if it were declared locally, but object z cannot. Object y can invoke x.c(), as can an object instantiated from any class in package One, such as Another1. Object z cannot invoke x.c(). These rules generalize in the same way for variables. The visibility rules may appear complicated initially, but they can be mastered with a little effort. package One class P P x = new P(); public a() protected b() c() private d() C1 y = new C1(); C2 z = new C2(); class Another1 package Two class C1 class C2 class Another2 FIG URE E .2 A situation demonstrating Java visibility modifiers 6114_LEWI_AppE_685-688.qxd 2/1/08 8:19 AM Page 687 AP P END I X E Other Java Modifiers Figure E.3 summarizes the rest of the Java modifiers, which address a variety of issues. Furthermore, a modifier has different effects on classes, interfaces, methods, and variables. Some modifiers cannot be used with certain constructs and therefore are listed as not applicable (N/A). The transient modifier is used to indicate data that need not be stored in a persistent (serialized) object. That is, when an object is written to a serialized stream, the object representation will include all data that is not specified as transient. Modifier Class abstract The class may contain abstract methods. It cannot be instantiated. Interface Method Variable No method body is defined. The method requires implementation when inherited. N/A The class cannot be N/A used to drive new classes. The method cannot be overridden. The variable is a constant, whose value cannot be changed once initially set. native N/A N/A No method body is necessary since implementation is in another language. N/A static N/A N/A Defines a class method. It does not require an instantiated object to be invoked. It cannot reference nonstatic methods or variables. It is implicitly final. Defines a class variable. It does not require an instantiated object to be referenced. It is shared (common memory space) among all instances of the class. synchro- N/A nized N/A The execution of the method is mutually exclusive among all threads. N/A transient N/A N/A N/A The variable will not be serialized. volatile N/A N/A N/A The variable is changed asynchronously. The compiler should not perform optimizations on it. final All interfaces are inherently abstract. The modifier is optional. FIG URE E .3 The rest of the Java modifiers Java Modifiers 687 6114_LEWI_AppE_685-688.qxd 2/1/08 8:19 AM Page 688