嵌套类有静态变量吗?

huangapple go评论65阅读模式
英文:

nested class has a static variable?

问题

我知道嵌套类没有静态成员,

class OuterClass {
     class InnerClass {
      private static int x = 0; // 这是错误的
     }

}

但是当我们用final声明它时,

 class OuterClass {
     class InnerClass {
      private static final int x = 0; // 可以
     }
}

这与final关键字有关吗,因为变量不能再被更改?
还是有其他原因?

英文:

I know that nested class has not had static member,

class OuterClass {
     class InnerClass {
      private static int x = 0; // this is error
     }

}

But when we declared it with final

 class OuterClass {
     class InnerClass {
      private static final int x = 0; // ok
     }
}

is it related with the final keyword, because variable can't be changed anymore?
Or is there another reason for this?

答案1

得分: 1

这种行为是由Java语言规范所规定的:

> 8.1.3. 内部类和封闭实例
> ---
>
> 内部类是嵌套类,它没有被明确或隐式地声明为静态。
>
> 内部类可以是非静态成员类(§8.5),局部类(§14.3)或匿名类(§15.9.5)。接口的成员类被隐式地声明为静态(§9.5),因此从不被视为内部类。
>
> 如果内部类声明了静态初始化程序,则会在编译时出现错误(§8.7)。
>
> 如果内部类声明了明确或隐式的静态成员,除非该成员是常量变量(§4.12.4),否则会在编译时出现错误。
>
> 内部类可以继承静态成员,即使不能声明这些静态成员。
>
> 不是内部类的嵌套类可以根据Java编程语言的通常规则自由声明静态成员。
>> 示例 8.1.3-1. 内部类声明和静态成员
>>
>> class HasStatic {
>> static int j = 100;
>> }
>> class Outer {
>> class Inner extends HasStatic {
>> static final int x = 3; // 可以:常量变量
>> static int y = 4; // 编译时错误:内部类
>> }
>> static class NestedButNotInner{
>> static int z = 5; // 可以:不是内部类
>> }
>> interface NeverInner {} // 接口从不是内部类
>> }

需要注意,这在Java 16中发生了变化:

> 所有适用于嵌套类的规则同样适用于内部类。特别地,内部类可以声明和继承static成员(§8.2),并声明静态初始化程序(§8.7),尽管内部类本身并非static

英文:

This behavior is mandated by the Java Language Specification:

> 8.1.3. Inner Classes and Enclosing Instances
> ---
>
> An inner class is a nested class that is not explicitly or implicitly declared static.
>
> An inner class may be a non-static member class (§8.5), a local class (§14.3), or an anonymous class (§15.9.5). A member class of an interface is implicitly static (§9.5) so is never considered to be an inner class.
>
> It is a compile-time error if an inner class declares a static initializer (§8.7).
>
> It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (§4.12.4).
>
> An inner class may inherit static members that are not constant variables even though it cannot declare them.
>
> A nested class that is not an inner class may declare static members freely, in accordance with the usual rules of the Java programming language.
>> Example 8.1.3-1. Inner Class Declarations and Static Members
>>
>> class HasStatic {
>> static int j = 100;
>> }
>> class Outer {
>> class Inner extends HasStatic {
>> static final int x = 3; // OK: constant variable
>> static int y = 4; // Compile-time error: an inner class
>> }
>> static class NestedButNotInner{
>> static int z = 5; // OK: not an inner class
>> }
>> interface NeverInner {} // Interfaces are never inner
>> }

Note that this changed with Java 16:

> All of the rules that apply to nested classes apply to inner classes. In particular, an inner class may declare and inherit static members (§8.2), and declare static initializers (§8.7), even though the inner class itself is not static.

答案2

得分: 0

因为 private static final int x = 0; 是编译时常量。
根据 Java 文档,

内部类不能声明静态成员,除非它们是编译时常量字段。

英文:

Because private static final int x = 0; is compile time constant.
According java doc,

> Inner classes may not declare static members, unless they are
> compile-time constant fields

huangapple
  • 本文由 发表于 2020年4月10日 19:50:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61139658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定