分类: Java
2010-07-13 17:49:30
Hist:
1007013, draft
Class declarations define new reference types and describe how they are implemented. A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class.
Class Declaration
A class declaration specifies a new named reference type. Two kinds of class declarations – normal class declarations and enum declarations:
ClassDeclaration:
NormalClassDeclaration
EnumDeclaration
NormalClassDeclaration:
ClassModifiersopt class Identifier
TypeParametersopt Superopt Interfacesopt ClassBody
ClassModifiers:
ClassModifier
ClassModifiers
ClassModifier
ClassModifier: one of
Annotation public
protected private abstract static final strictfp
abstract Classes
An abstract class is a class that is incomplete, or to be considered incomplete. Normal class may have abstract methods, that is methods that are declared but not yet implemented, only if they are abstract classes.
Enum types must not be declared abstract.
A class C has abstract methods if any of the following is true:
1) C explicitly contains a declaration of an abstract method
2) any of C’s superclasses has an abstract method and C neither declares nor inherits a method that implements it.
3) a direct superinterface of C delcares or inherits a method and C neither declares nor inherits a method that implements it.
final Classes
A class can be declared final if its definition is complete and no subclasses are desired or required. abstract and final cannot be used together.
strictfp Classes
The effect of the strictfp modifier is to make all float or double expressions within the class declaration be explicitly.
Generic Classes and Type Parameters
A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class.
It is a compile-time error if a generic class is a direct or indirect subclass of Throwable since the catch mechanism of the Java vm works only with non-generic classes.
The scope of a class’ type parameter is the entire declaration of the class including the type parameter section itself.
Inner Classes and Enclosing Instances
An inner class is a nested class that is not explicitly or implicitly declared static. Inner class may not declare static initialzers or member interface. Inner classes may not declare static members, unless they are compile-time constant fields.
Any instance of an inner class I whose declaration occurs in a static context has no lexically enclosing instances. Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final.
Inner classes include local, anonymous and non-static member classes.
Inner classes whose declarations do not occur in a static context may freely refer to the instance variables of their enclosing class.
Superclasses and Subclasses
Super:
extends ClassType
ClassType:
TypeDeclSpecifier
TypeArgumentsopt
The direct superclass of an enum type E is
Enum
The extends clause must not appear in the definition of the class Object, because it is primordial class and has no direct superclass.
Superinterfaces
Class Body and Member Declarations
Class Members
Members of a class that are declared private are not inherited by subclasses of the class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.
We use the phrase the type of a member to denote:
1) for a field, its type
2) for a method, an ordered 3-tuple consisting of:
a) argument types: a list of the types of the arguments to the method member.
b) return type: the return type of the method member and the
c) throw clause: exception types declared in the throws clause of the method member
Constructors, static initializers, and instance initializers are not members and therefore are not inherited.
Field Declarations
Field Modifiers
FieldModifiers:
FieldModifier
FieldModifiers
FieldModifier
FieldModifier: one of
Annotation public
protected private static final transient volatile
static Fields
If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized. A field that is not declared static is called an instance variable.
final Fields
It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared. A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared.
transient Fields
Variables may be marked transient to indicate that they are not part of the persistent state of an object.
volatile Fields
A field may be declared volatile in which case the java memory model ensures that all threads see consistent value for the variable.
final and volatile cannot be used together.
Initialization of Fields
Method Declarations
A method declares executable code that can be invoked, passing a fixed number of values as arguments.
It is a compile-time error for the body of a class to declare as members two methods with override-equivalent signature(name, number of parameters, and types of any parameters).
Formal Parameters
Method Signature
Two methods have the same signature if they have the same name and argument types:
1) they have the same number of formal parameters
2) they have the same number of type parameters
3)
let
The signature of a method m1 is a subsignature of the signature of a method m2 if eithere
1) m2 has the same signature as m1, or
2) the signature of m1 is the same as the erasure of the signature of m2.
Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1.
Method Modifiers
MethodModifiers:
MethodModifier
MethodModifiers
MethodModifier
MethodModifier: one of
Annotation public
protected private abstract static final synchronized native strictfp
final Methods
A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.
native Methods
A method that is native is implemented in platform-dependent code, typically writtent in another programming language such as C, C++, or assembly language. The body of a native method is given as a semicolon only. native and abstract cannot be used together.
synchronized Methods
A synchronized method acquires a monitor before it executes. For a class method, the monitor associated with Class object for the method’s class is used. For an instance method, the monitor associated with this (object) is used.
Generic Methods
A method is generic if it declares one or more type variables. These type variable are known as the formal type parameters of method.
Method Return Type
Method Throws
A throws clause is used to declare any checked exceptions that can result from the execution of a method or constructor.
For each checked exception that can result from execution of the body of a method or constructor, a compile-time error occurs unless that exception type or a supertype of that exception type is mentioned in a throws clause in the declaration of the method or constructor.
The requirement to declare checked exceptions allows the compiler to ensure that code for handling such error conditions has been included.
The exception that are not checked are those for which declaring every possible occurrence would be unimaginably inconvenient:
1) exceptions that are represented by the subclasses of class Error.
2) the exceptions that are represented by the subclasses of the class RuntimeException.
A method that overrides or hides another method, including methods that implements abstract methods defined in interfaces, may not be declared to throw more checked exception than the overridden or hidden method.
Method Body
Inheritance, Overriding, and Hiding
Overriding(by Instance Methods)
An instance method m1 declared in a class C overrides another instance method, m2, declared in class A iff all of the following are true:
1) C is a subclass of A
2) the signature of m1 is a subsignature of the signature of m2.
3) either
a) m2 is public, protected or declared with default access in the same package as C, or
b) m1 overrides a method m3, m3 is distict from m1, m3 distinct from m2, such that m3 overrides m2.
An overridden method can be accessed by using a method invocation expression that contains the keyword super.
Hiding(by Class Methods)
If a class declares a static method m, then the declaration m is said to hide any method mi, where the signature of m is a subsignature of the signature of mi, in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class.
Overloading
If two methods of a class have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded
Member Type Declarations
Constructor Declarations
Enums
EnumDeclaration:
ClassModifiersopt enum Identifier Interfacesopt EnumBody
EnumBody:
{ EnumConstantsopt ,opt EnumBodyDeclarationsopt }
The body of an enum type may contain enum constants. An enum constant defines an instance of the enum type. An enum type has no instances other than those defined by its enum constants. It is a compile-time error to attempt to explicitly instantiate an enum type.
An enum type is implicitly final unless it contains at least one enum constant that has a class body. Nested enum types are implicitly static.
1. JLS 3nd