Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
| 4.1 | (c)
The code will fail to compile, since the package declaration cannot occur after an import statement. The package and import statements, if present, must always precede any type declarations. If a file contains both import statements and a package statement, the package statement must occur before the import statements. |
| 4.2 | (c) and (e)
The name of the class must be fully qualified. A parameter list after the method name is not permitted. (c) illustrates single static import and (e) illustrates static import on demand. |
| 4.3 | (a) and (b)
(a) imports all types from the package java.util, including the type java.util.Locale. (b) explicitly imports the type java.util.Locale, which is what is needed. (c) is syntactically incorrect, as java.util.Locale.UK is not a type. (d) imports types from the package java.util.Locale, but not the type java.util.Locale. In (e), the static import is specified from a package (java.util), and not from a type, as required. In (f), the static import is incorrectly specified for a type (java.util.Locale) and not for a static member. Both (g) and (h) with static import do not work, because we are referring to the constant UK using the simple name of the class (Locale) in the main() method. |
| 4.4 | (f)
The enum type Signal is not visible outside the package p1. If it were, (b), (c) and (d) would work. No static import is really necessary, since the constants of the enum type Signal are not used in the package p2. |
| 4.5 | (a), (b), and (c)
(d) does not statically import p3.Util.print. Note that p3.Util.Format can be imported as a type and as a static member from p3.Util, as in (a) and (b), respectively. |
| 4.6 | (b) and (e)
Static import from a class does not automatically import static members of any nested types declared in that class. The order of the import statements is arbitrary as long as it is delcared after any package statement and before any type declaration. Name conflicts must be disambiguated explicitly. |
| 4.7 | (b), (d), and (f)
In (a), the file A.class will be placed in the same directory as the file A.java. There is no -D option for the javac command, as in (c). The compiler maps the package structure to the file system, creating the necessary (sub)directories. |
| 4.8 | (b) and (d)
In (a) and (c), class A cannot be found. In (e) and (f), class B cannot be found —there is no package under the current directory /top/wrk/pkg to search for class B. Note that specifying pkg in the classpath in (d) is superfluous. The parent directory of the package must be specified, i.e., the location of the package. |
| 4.9 | (d) and (f)
The parent directory (or location) of the package must be specified. Only (d) and (f) do that. (d) specifies the current directory as well, but the search is from left to right in the specified paths, resulting in the top.sub.A class being found. |
| 4.10 | (a) and (c)
There is no -d option for the java command, as in (b). There should be no white space between the -D option and the name-value pair, as in (d), and no white space around the = sign either. The value can be quoted, especially if it contains white space. |
| 4.11 | (e) |
| 4.12 | (c) and (d)
A class or interface name can be referred to by using either its fully qualified name or its simple name. Using the fully qualified name will always work, but in order to use the simple name it has to be imported. By importing net.basemaster.* all the type names from the package net.basemaster will be imported and can now be referred to using simple names. Importing net.* will not import the subpackage basemaster. |
| 4.13 | (c)
Any normal class can be declared abstract. A class cannot be instantiated if the class is declared abstract. The declaration of an abstract method cannot provide an implementation. The declaration of a non-abstract method must provide an implementation. If any method in a class is declared abstract, then the class must be declared abstract, so (a) is invalid. The declaration in (b) is not valid, since it omits the keyword abstract in the method declaration. The declaration in (d) is not valid, since it omits the keyword class. |
| 4.14 | (e)
A class can be extended unless it is declared final. For classes, final means it cannot be extended, while for methods, final means it cannot be overridden in a subclass. A nested static class, (d), can be extended. A private member class, (f), can also be extended. The keyword native can only be used for methods, not for classes and fields. |