Mastering Advanced Language Features Part

1. A nested class is a class that is declared as a member of another class or scope.

2. The four kinds of nested classes are static member classes, nonstatic member classes, anonymous classes, and local classes.

3. Nonstatic member classes, anonymous classes, and local classes are also known as inner classes.

4. The answer is false: a static member class does not have an enclosing instance.

5. You instantiate a nonstatic member class from beyond its enclosing class by first instantiating the enclosing class, and then prefixing the new operator with the enclosing class instance as you instantiate the enclosed class. Example: new EnclosingClass().new EnclosedClass().

6. It is necessary to declare local variables and parameters final when they are being accessed by an instance of an anonymous class or a local class.

7. The answer is true: an interface can be declared within a class or within another interface.

8. A package is a unique namespace that can contain a combination of top-level classes, other top-level types, and subpackages.

9. You ensure that package names are unique by specifying your reversed Internet domain name as the top-level package name.

10. A package statement is a statement that identifies the package in which a source file's types are located.

11. The answer is false: you cannot specify multiple package statements in a source file.

12. An import statement is a statement that imports types from a package by telling the compiler where to look for unqualified type names during compilation.

13. You indicate that you want to import multiple types via a single import statement by specifying the wildcard character (*).

14. During a runtime search, the virtual machine reports a "no class definition found" error when it cannot find a classfile.

15. You specify the user classpath to the virtual machine via the -classpath option used to start the virtual machine or, if not present, the CLASSPATH environment variable.

16. A constant interface is an interface that only exports constants.

17. Constant interfaces are used to avoid having to qualify their names with their classes.

18. Constant interfaces are bad because their constants are nothing more than an implementation detail that should not be allowed to leak into the class's exported interface, because they might confuse the class's users (what is the purpose of these constants?). Also, they represent a future commitment: even when the class no longer uses these constants, the interface must remain to ensure binary compatibility.

19. A static import statement is a version of the import statement that lets you import a class's static members so that you do not have to qualify them with their class names.

20. You specify a static import statement as import, followed by static, followed by a member access operator-separated list of package and subpackage names, followed by the member access operator, followed by a class's name, followed by the member access operator, followed by a single static member name or the asterisk wildcard; for example, import static java.lang.Math.cos; (import the cos() static method from the Math class).

21. An exception is a divergence from an application's normal behavior.

22. Objects are superior to error codes for representing exceptions because error code Boolean or integer values are less meaningful than object names, and because objects can contain information about what led to the exception. These details can be helpful to a suitable workaround.

23. A throwable is an instance of Throwable or one of its subclasses.

24. The getCause() method returns an exception that is wrapped inside another exception.

25. Exception describes exceptions that result from external factors (such as not being able to open a file) and from flawed code (such as passing an illegal argument to a method). Error describes virtual machine-oriented exceptions such as running out of memory or being unable to load a classfile.

26. A checked exception is an exception that represents a problem with the possibility of recovery, and for which the developer must provide a workaround.

27. A runtime exception is an exception that represents a coding mistake.

28. You would introduce your own exception class when no existing exception class in Java's class library meets your needs.

29. The answer is false: you use a throws clause to identify exceptions that are thrown from a method by appending this clause to a method's header.

30. The purpose of a try statement is to provide a scope (via its brace-delimited body) in which to present code that can throw exceptions. The purpose of a catch clause is to receive a thrown exception and provide code (via its brace-delimited body) that handles that exception by providing a workaround.

31. The purpose of a finally clause is to provide cleanup code that is executed whether an exception is thrown or not.

32. Listing 15 presents the G2D class that was called for in Chapter 3. Listing 15. The G2D class with its Matrix nonstatic member class public class G2D {

private Matrix xform;

xform = new Matrix(); xform.a = 1.0; xform.e = 1.0; xform.i = 1.0;

private class Matrix {

33. To extend the logging package to support a null device in which messages are thrown away, first introduce Listing 16's NullDevice package-private class.

Listing 16. Implementing the proverbial "bit bucket" class package logging;

class NullDevice implements Logger {

private String dstName;

NullDevice(String dstName) {

public boolean connect() {

return true;

public boolean disconnect()

return true;

public boolean log(String msg) {

return true;

Continue by introducing, into the LoggerFactory class, a NULLDEVICE constant and code that instantiates NullDevice with a null argument—a destination name is not required—when newLogger()'s dstType parameter contains this constant's value. Check out Listing 17.

Listing 17. A refactored LoggerFactory class package logging;

public abstract class LoggerFactory {

public final static int CONSOLE = 0; public final static int FILE = 1; public final static int NULLDEVICE = 2;

public static Logger newLogger(int dstType, String...dstName) {

switch (dstType) {

case CONSOLE : return new Console(dstName.length == 0 ? null

case FILE : return new File(dstName.length == 0 ? null

case NULLDEVICE: return new NullDevice(null); default : return null;

34. Modifying the logging package so that Logger's connect() method throws a CannotConnectException instance when it cannot connect to its logging destination, and the other two methods each throw a NotConnectedException instance when connect() was not called or when it threw a CannotConnectException instance, results in Listing 18's Logger interface.

Listing 18. A Logger interface whose methods throw exceptions package logging;

public interface Logger {

void connect() throws CannotConnectException; void disconnect() throws NotConnectedException; void log(String msg) throws NotConnectedException;

Listing 19 presents the CannotConnectException class.

Listing 19. An uncomplicated CannotConnectException class package logging;

public class CannotConnectException extends Exception {

The NotConnectedException class has a similar structure. Listing 20 presents the Console class. Listing 20. The Console class satisfying Logger's contract without throwing exceptions package logging;

class Console implements Logger {

private String dstName; Console(String dstName)

this.dstName = dstName;

public void connect() throws CannotConnectException public void disconnect() throws NotConnectedException public void log(String msg) throws NotConnectedException System.out.println(msg);

Listing 21 presents the File class. Listing 21. The File class satisfying Logger's contract by throwing exceptions as necessary package logging;

class File implements Logger {

private String dstName; File(String dstName)

this.dstName = dstName;

public void connect() throws CannotConnectException if (dstName == null)

throw new CannotConnectException();

public void disconnect() throws NotConnectedException if (dstName == null)

throw new NotConnectedException();

public void log(String msg) throws NotConnectedException

throw new NotConnectedException(); System.out.println("writing " + msg + " to file " + dstName);

35. When you modify TestLogger to respond appropriately to thrown

CannotConnectException and NotConnectedException objects, you end up with something similar to Listing 22.

Listing 22. A TestLogger class that handles thrown exceptions import logging.*;

public class TestLogger {

public static void main(String[] args) {

Logger logger = LoggerFactory.newLogger(LoggerFactory.CONSOLE);

logger.connect();

logger.log("test message #1");

logger.disconnect();

catch (CannotConnectException cce) {

System.err.println("cannot connect to console-based logger");

catch (NotConnectedException nce) {

System.err.println("not connected to console-based logger");

Logger logger = LoggerFactory.newLogger(LoggerFactory.FILE, "x.txt");

logger.connect();

logger.log("test message #2");

logger.disconnect();

catch (CannotConnectException cce) {

System.err.println("cannot connect to file-based logger");

catch (NotConnectedException nce) {

System.err.println("not connected to file-based logger");

Logger logger = LoggerFactory.newLogger(LoggerFactory.FILE);

logger.connect();

logger.log("test message #3");

logger.disconnect();

catch (CannotConnectException cce)

System.err.println("cannot connect to file-based logger"); catch (NotConnectedException nce)

System.err.println("not connected to file-based logger");

Was this article helpful?

0 0

Post a comment