内部类可以在Java 17中拥有静态成员吗?

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

Inner classes can have the static members inside it in java 17?

问题

我正在阅读有关内部类的文章,出于好奇,我在内部类中声明了静态成员。奇怪的是,没有显示编译时错误,代码正常执行。

我回头查看了Oracle文档,仍然找到了这样的语句:'因为内部类与实例关联,它本身不能定义任何静态成员。'

我真的很困惑,是否在后续版本的Java中对内部类进行了增强,使我们现在也可以在内部类中声明静态成员?

对此的任何说明将不胜感激。

以下是您可以尝试在Java 17中使用的代码片段:

class Outer{
     class Inner{
          static int i =10;
          public static void method(){
              System.out.println("现在可以在内部类中声明静态成员了!!!!");
          }
     }
}
英文:

I was reading the articles regarding the inner classes and out of curiosity I declared the static members within the inner class. Strangely no compile time error was shown and the code just executed fine.

I referred back the oracle documents and could still found the statement : 'because an inner class is associated with an instance, it cannot define any static members itself.'

I am really confused, if there is an enhancement to the inner classes done with the later versions of java where we can now declare the static members within the inner classes as well?

Any light on that will be appreciated.

Here is the code snippet you may try with Java 17:

class Outer{
     class Inner{
          static int i =10;
          public static void method(){
              System.out.println("The static members can be now declared within Inner classes!!!!");
          }
     }
}

答案1

得分: 1

从Oracle文档中:

> Java教程是针对JDK 8编写的。本页面中描述的示例和实践未充分利用后续版本中引入的改进,并可能使用不再可用的技术。

关于放宽静态成员规则的提案来自openjdk.org

> 决议:已批准。
> 修复版本:16

所以,是的,内部类允许拥有静态成员,但Oracle文档是针对Java 8及以下版本编写的。自Java 16以来,规则已发生变化。

英文:

From the Oracle docs:

> The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.

And a proposal to relax rules of static members, from openjdk.org:

> Resolution: Approved.
> Fix Version/s: 16

So, yeah, it's allowed for inner classes to have static members, but the Oracle documentation was written to include features of up to Java 8. The rules have changed since Java 16.

答案2

得分: 1

从Java 6语言规范中。

内部类是一个嵌套类,它没有明确或隐式地声明为静态。内部类不可以声明静态初始化器(§8.7)或成员接口。内部类不可以声明静态成员,除非它们是编译时常量字段(§15.28)。

为了说明这些规则,考虑下面的示例:(摘自Java 6 LS)

class HasStatic {
    static int j = 100;
}
class Outer {
    class Inner extends HasStatic {
        static final int x = 3; // ok - 编译时常量
        static int y = 4; // 编译时错误,内部类
    }
    static class NestedButNotInner {
        static int z = 5; // ok,不是内部类
    }
    interface NeverInner {} // 接口永远不是内部类
}

这在JEP 395中得以放宽,该JEP最终确定了将“record”作为语言的标准特性。该声明如下:

本JEP建议在JDK 16中完成该特性,具体的细化如下:

放宽长期以来的限制,即内部类不能声明明确或隐式静态的成员。这将变得合法,并且特别允许内部类声明一个记录类的成员。

英文:

From Java 6 language specification.

> An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).

To illustrate these rules, consider the example below: (Taken from the Java 6 LS)

class HasStatic{
	static int j = 100;
}
class Outer{
	class Inner extends HasStatic{
		static final int x = 3;			// ok - compile-time constant
		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
}

This was relaxed in JEP 395 that finalized the inclusion of record as a standard feature of the language. The statement reads:

>This JEP proposes to finalize the feature in JDK 16, with the following refinement:
>
>Relax the longstanding restriction whereby an inner class cannot declare a member that is explicitly or implicitly static. This will become legal and, in particular, will allow an inner class to declare a member that is a record class.

huangapple
  • 本文由 发表于 2023年7月23日 23:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748971.html
匿名

发表评论

匿名网友

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

确定