Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_language_specification_7.pdf
Скачиваний:
28
Добавлен:
21.03.2016
Размер:
3.11 Mб
Скачать

CLASSES

Method Declarations

8.4

};

}

int w = x = 3;

//ok - x at left hand side of assignment int p = x;

//ok - instance initializers may access static fields

static int u =

(new Object() { int bar() { return x; } }).bar(); // ok - occurs in a different class

static int x;

int m = j = 4;

// ok - j at left hand side of assignment int o =

(new Object() { int bar() { return j; } }).bar(); // ok - occurs in a different class

int j;

}

8.4 Method Declarations

A method declares executable code that can be invoked, passing a fixed number of values as arguments.

MethodDeclaration:

MethodHeader MethodBody

MethodHeader:

MethodModifiersopt TypeParametersopt Result MethodDeclarator Throwsopt

MethodDeclarator:

Identifier ( FormalParameterListopt )

The FormalParameterList is described in §8.4.1, the MethodModifiers clause in §8.4.3, the TypeParameters clause in §8.4.4, the Result clause in §8.4.5, the Throws clause in §8.4.6, and the MethodBody in §8.4.7.

The Identifier in a MethodDeclarator may be used in a name to refer to the method.

It is a compile-time error for the body of a class to declare as members two methods with override-equivalent signatures (§8.4.2).

The scope and shadowing of a method declaration is specified in §6.3 and §6.4.

215

8.4.1

Formal Parameters

CLASSES

For compatibility with older versions of the Java SE platform, the declaration of a method that returns an array is allowed to place (some or all of) the empty bracket pairs that form the declaration of the array type after the formal parameter list. This is supported by the following obsolescent production, but should not be used in new code.

MethodDeclarator:

MethodDeclarator [ ]

8.4.1 Formal Parameters

The formal parameters of a method or constructor, if any, are specified by a list of comma-separated parameter specifiers. Each parameter specifier consists of a type (optionally preceded by the final modifier and/or one or more annotations) and an identifier (optionally followed by brackets) that specifies the name of the parameter.

The last formal parameter of a method or constructor is special: it may be a variable arity parameter, indicated by an ellipsis following the type.

If the last formal parameter is a variable arity parameter, the method is a variable arity method. Otherwise, it is a fixed arity method.

If a method or constructor has no formal parameters, only an empty pair of parentheses appears in the declaration of the method or constructor.

216

CLASSES

Formal Parameters

8.4.1

FormalParameterList:

LastFormalParameter

FormalParameters , LastFormalParameter

FormalParameters:

FormalParameter

FormalParameters , FormalParameter

FormalParameter:

VariableModifiersopt Type VariableDeclaratorId

VariableModifiers:

VariableModifier

VariableModifiers VariableModifier

VariableModifier: one of

Annotation final

LastFormalParameter:

VariableModifiersopt Type... VariableDeclaratorId

FormalParameter

The following is repeated from §8.3 to make the presentation here clearer:

VariableDeclaratorId:

Identifier

VariableDeclaratorId [ ]

If an annotation a (§9.7)

on a

formal parameter

corresponds to an

annotation type T (§9.6), and

T has

a (meta-)annotation

m that corresponds

to java.lang.annotation.Target, then m must have an element whose value is java.lang.annotation.ElementType.PARAMETER, or a compile-time error occurs.

The scope and shadowing of a formal parameter is specified in §6.3 and §6.4.

It is a compile-time error for a method or constructor to declare two formal parameters with the same name. (That is, their declarations mention the same

Identifier.)

It is a compile-time error if a formal parameter that is declared final is assigned to within the body of the method or constructor.

217

8.4.1

Formal Parameters

CLASSES

It is a compile-time error to use mixed array notation (§10.2) for a variable arity parameter.

The declared type of a formal parameter is denoted by the Type that appears in its parameter specifier, followed by any bracket pairs that follow the Identifier in the declarator, except for a variable arity parameter, whose declared type is an array type whose component type is the Type that appears in its parameter specifier.

If the declared type of a variable arity parameter has a non-reifiable element type (§4.7), then a compile-time unchecked warning occurs for the declaration of the variable arity method, unless the method is annotated with the SafeVarargs annotation (§9.6.3.7) or the unchecked warning is suppressed by the SuppressWarnings annotation (§9.6.3.5).

When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor. The Identifier that appears in the DeclaratorId may be used as a simple name in the body of the method or constructor to refer to the formal parameter.

Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation (§15.12.4.2).

A method or constructor parameter of type float always contains an element of the float value set (§4.2.3); similarly, a method or constructor parameter of type double always contains an element of the double value set. It is not permitted for a method or constructor parameter of type float to contain an element of the float- extended-exponent value set that is not also an element of the float value set, nor for a method parameter of type double to contain an element of the double-extended- exponent value set that is not also an element of the double value set.

Where an actual argument expression corresponding to a parameter variable is not FP-strict (§15.4), evaluation of that actual argument expression is permitted to use intermediate values drawn from the appropriate extended-exponent value sets. Prior to being stored in the parameter variable, the result of such an expression is mapped to the nearest value in the corresponding standard value set by method invocation conversion (§5.3).

218

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]