为什么允许实例化静态嵌套类对象? [英] Why instantiation of static nested class object is allowed?

查看:132
本文介绍了为什么允许实例化静态嵌套类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习用于Android应用程序开发的Java语言。

I have started learning Java language for Android Application developement.

根据我对静态类的理解,我们无法实例化静态类的对象。

As per my understanding based on static class, we cannot instantiate object of static class.

但为什么在下面的situaltion中允许实例化静态嵌套类对象?

But why instantiation of static nested class object is allowed in following situaltion?

class EnclosingClass 
{     
      //...     
      class static StaticInnerClass 
      {         
          //...     
      } 
} 

为什么我们可以创建内部类的对象,如果它被标记为静态?

Why we can create object of inner class if it is marked as static?

EnclosingClass.StaticInnerClass s = new EnclosingClass.StaticInnerClass()


推荐答案


根据我对静态类的理解,我们无法实例化静态类的对象。

As per my understanding based on static class, we cannot instantiate object of static class.

您对静态类含义的理解不正确。基本上, Java 中的静态类是一个嵌套类,它没有对包含类的实例的隐式引用。请参见第8.5.1节JLS 获取更多信息,特别是:

Your understanding of the meaning of "static class" is incorrect. Basically a "static class" in Java is a nested class which doesn't have an implicit reference to an instance of the containing class. See section 8.5.1 of the JLS for more information, in particular:


static关键字可能会修改成员类型C的声明一个非内部类或接口T.它的作用是声明C不是内部类。正如T的静态方法在其体内没有T的当前实例一样,C也没有T的当前实例,也没有任何词法封闭实例。

The static keyword may modify the declaration of a member type C within the body of a non-inner class or interface T. Its effect is to declare that C is not an inner class. Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have any lexically enclosing instances.

也许你在想C#中的静态类,它们完全不同?

Perhaps you were thinking of static classes in C#, which are completely different?

这篇关于为什么允许实例化静态嵌套类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆