Download 5.4 Class Declarations

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
VARIANT - № 12
Local classes are most often used for adapter classes. An adapter class is a class that implements
a particular interface, so that another class can call a particular method in the adapter class when
a certain event occurs. In other words, an adapter class is Java's way of implementing a
"callback" mechanism. Adapter classes are commonly used with the new event-handling model
required by the Java 1.1 AWT and by the JavaBeans API.
To tell a Button object that you want to be notified when it is pressed, you pass an instance of an
adapter class that implements the ActionListener interface to its addActionListener()
method. A class that implements the ActionListener interface is required to implement the
actionPerformed() method. When the Button is pressed, it calls the adapter object's
actionPerformed() method. The main advantage of declaring the ButtonNotifier class in the
method that creates the Button is that it puts all of the code related to creating and setting up the
Button in one place.
As the preceding example shows, a local class can access local variables of the block in which it
is declared. However, any local variables that are accessed by a local class must be declared
final. A local class can also access method parameters and the exception parameter of a catch
statement that are accessible within the scope of its block, as long as the parameter is declared
final. The Java compiler complains if a local class uses a non-final local variable or
parameter. The lifetime of a parameter or local variable is extended indefinitely, as long as there
is an instance of a local class that refers to it.
Anonymous classes
An anonymous class is a kind of local class that does not have a name and is declared inside of
an allocation expression. As such, an anonymous class is a more concise declaration of a local
class that combines the declaration of the class with its instantiation.
An anonymous class is declared as part of an allocation expression. If the name after new is the
name of an interface, as is the case in the preceding example, the anonymous class is an
immediate subclass of Object that implements the given interface. If the name after new is the
name of a class, the anonymous class is an immediate subclass of the named class.
Obviously, an anonymous class doesn't have a name. The other restriction on an anonymous
class is it can't have any constructors other than the default constructor. Any constructor-like
initialization must be done using an instance initializer. Other than these differences, anonymous
classes function just like local classes.
Implementation of inner classes
It is possible to use inner classes without knowing anything about how they are implemented.
However, a high-level understanding can help you comprehend the filenames that the compiler
produces, and also some of the restrictions associated with inner classes. The implementation of
inner classes is less than transparent in a number of ways, primarily because the Java virtual
machine does not know about inner classes. Instead, the Java compiler implements inner classes
by rewriting them in a form that does not use inner classes. The advantage of this approach is
that the Java virtual machine does not require any new features to be able to run programs that
use inner classes.
Since a class declared inside another class is rewritten by the compiler as an external class, the
compiler must give it a name unique outside of the class in which it is declared. The unique
name is formed by prefixing the name of the inner class with the name of the class in which it is
declared and a dollar sign ($). Thus, when the Queue class is compiled, the Java compiler
produces four .class files:
 Queue.class
 Queue$EmptyQueueException.class
 Queue$QueueEnumerator.class
 Queue$QueueNode.class
Because anonymous classes do not have names, the Java compiler gives each anonymous class a
number for a name; the numbers start at 1. When the version of the Z applet that uses an
anonymous class is compiled, the Java compiler produces two .class files:
 Z.class
 Z$1.class
In order to give an inner class access to the variables of its enclosing instance, the compiler adds
a private variable to the inner class that references the enclosing instance. The compiler also
inserts a formal parameter into each constructor of the inner class and passes the reference to the
enclosing instance using this parameter.
The compiler rewrites all references to the enclosing instance as this$0. One implication of this
implementation is that you cannot pass the enclosing instance as an argument to its superclass's
constructor because this$0 is not available until after the superclass's constructor returns.
5.4 Class Declarations
A class declaration creates a reference type in Java. The class declaration also specifies the
implementation of the class, including its variables, constructors, and methods. The formal
definition of a class declaration is:
While the above diagram may seem complicated, a class declaration is really made up of six
distinct things:
 Optional modifiers that specify attributes of the class
 The keyword class
 An identifier that names the class
 An optional extends clause that specifies the superclass of the declared class
 An optional implements clause that specifies the interfaces implemented by the declared
class
 Any number of member declarations, which can include variables, methods, constructors,
static initializers, instance initializers, nested top-level classes and interfaces, and
member classes
Class Modifiers
The keywords public, abstract, and final can appear as modifiers at the beginning of a class
declaration. In this situation, these modifiers have the following meanings:[3]
Version 1.0 of Java included a private protected access specification; this specification has
been removed as of version 1.0.2 of the language.
Public If a class is declared public, it can be referenced by any other class. If the public
modifier is not used, however, the class can only be referenced by other classes in the same
package. A single source file, or compilation unit, can only declare one public class or interface
Abstract If a class is declared abstract, no instances of the class may be created. A class
declared abstract may contain abstract methods. Classes not declared abstract may not
contain abstract methods and must override any abstract methods they inherit with methods
that are not abstract. Furthermore, classes that implement an interface and are not declared
abstract must contain or inherit methods that are not abstract that have the same name,
number of parameters, and corresponding parameter types as the methods declared in the
interfaces that the class implements.
Final If a class is declared final, it cannot be subclassed. In other words, it cannot appear in
the extends clause of another class. You want to declare a class final if it is important to
ensure the exact properties and behavior of that class. Many of the classes in the java.lang
package are declared final for that reason. In addition, the compiler can often optimize
operations on final classes. For example, the compiler can optimize operations involving the
String class because it can safely assume the exact logic of String methods. The compiler does
not have to account for the possibility of methods of a final class being overridden in a
subclass.
Class Name
The identifier that follows the keyword class is the name of the class. This identifier can be
used as a reference type wherever the class is accessible.
Class Inheritance
The extends clause specifies the superclass of the class being declared. If a class is declared
without an extends clause, the class Object is its implicit superclass. The class inherits all of
the accessible methods and variables of its superclass.
If a class is declared final, it cannot appear in an extends clause for any other class.
The implements clause specifies any interfaces implemented by the class being declared. Unless
it is an abstract class, the class (or one of its superclasses) must define implementations for all
of the methods declared in the interfaces.
Class Members
Fields are the variables, methods, constructors, static (load-time) initializers, instance initializers,
nested top-level classes and interfaces, and member classes that are declared as part of a class:
A member declaration causes the member to be defined throughout the entire class and all of its
subclasses. This means that it is not a problem to have forward references to members, or in
other words, you can use members in a class before you have defined them.
Variables
A variable that is declared as a member in a class is called a field variable. A field variable is
different from a local variable, which is declared within a method or a block.
A variable declaration is really made up of four distinct things:
 Optional modifiers that specify attributes of the variable.
 A type, which can be either a primitive type or a reference type.
 Any number of identifiers that name variables. Each name can be followed by pairs of
square brackets to indicate an array variable.
 An optional initializer for each variable declared.
Variable modifiers
The modifiers public, protected, and private can be used in the declaration of a field
variable to specify the accessibility of the variable. In this situation, the modifiers have the
following meanings:
Version 1.0 of Java included a private protected access specification; this specification has
been removed as of version 1.0.2 of the language.
Public A field variable that is declared public is accessible from any class.
Protected A field variable that is declared protected is accessible to any class that is part of
the same package as the class in which the variable is declared. Such a field variable is also
accessible to any subclass of the class in which it is declared; this occurs regardless of whether or
not the subclass is part of the same package.
Private A field variable that is declared private is only accessible in the class in which it is
declared. Such a field variable is not accessible to other classes. In particular, a field variable that
is declared private is not accessible in subclasses of the class in which it is declared.
If a field variable is not declared with any of the access modifiers, the variable has the default
accessibility. Default access is often called "friendly" access because it is similar to friendly
access in C++. A variable with default access is accessible in any class that is part of the same
package as the class in which the variable is declared. However, a friendly variable is not
accessible to classes outside of the package in which it is declared, even if the desired classes are
subclasses of the class in which it is declared.
The keywords static, final, transient, and volatile can also be used in the declaration of a
field variable. These modifiers have the following meanings:
Static A field variable that is declared with the static modifier is called a class variable.
There is exactly one copy of each class variable associated with the class; every instance of the
class shares the single copy of the class's static variables. Thus, setting the value of a class
variable changes the value of the variable for all objects that are instances of that class or any of
its subclasses.
A field variable that is not declared with the static modifier is called an instance
variable. There is a distinct copy of each instance variable associated with every instance of the
class. Thus, setting the value of an instance variable in one object does not affect the value of
that instance variable in any other object.
Final If a field variable is declared with the final modifier, the variable is a named
constant value. As such, it must be assigned an initial value. Any assignment to a final variable,
other than the one that provides its initial value, is a compile-time error. The initial value for a
final variable is typically provided by an initializer that is part of the variable's declaration
A final field variable that is not initialized in its declaration is called a blank final.
Blank finals are not supported prior to Java 1.1. A blank final that is declared static must be
assigned a value exactly once in a static initializer. A blank final that is not declared static
must be assigned a value exactly once in an instance initializer or exactly once in each
constructor. The compiler uses flow analysis that takes if statements and iteration statements
into account to ensure that a blank final is assigned a value exactly once. Thus, it is possible to
have multiple assignments to a blank final, so long as exactly one of them can be executed. In
particular, if a class contains a final variable, you can declare a variable with the same name in
a subclass of that class without causing an error.