6.3. Scope of a Declaration
The scope of a declaration of a member m declared in or inherited by an interface type I (§9.1.4) is the entire body of I, including any nested type declarations.
interface I{
class C{}
static class D{}
}
The scope of a class's type parameter (§8.1.2) is the type parameter section of the class declaration, the type parameter section of any superclass or superinterface of the class declaration, and the class body.
The scope of an interface's type parameter (§9.1.2) is the type parameter section of the interface declaration, the type parameter section of any superinterface of the interface declaration, and the interface body.
interface IU<T>{ }
class CU<T>{}
public class main3<X extends Number> extends CU<X> implements IU<X> {
}
The scope of a constructor's type parameter (§8.8.4) is the entire declaration of the constructor, including the type parameter section, but excluding the constructor modifiers.
public class main3{
public <A,B extends A> main3(){
}
}
The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.
int a = 1,b = a;
The scope of a local variable declared in the ForInit part of a basic for statement (§14.14.1) includes all of the following:
ForInit、ForInitializer与ForUpdate
The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.
class Test1 {
static int x;
public static void main(String[] args) {
int x = x;
}
}
This program causes a compile-time error because the initialization of x is within the scope of the declaration of x as a local variable, and the local variable x does not yet have a value and cannot be used.
本文详细解析了Java中各种声明的范围规则,包括接口成员、类型参数、构造函数类型参数及局部变量的声明范围,提供了丰富的代码示例,帮助开发者深入理解Java的语法特性。
3093

被折叠的 条评论
为什么被折叠?



