内部类

作者: 关月003 | 来源:发表于2018-06-07 02:57 被阅读0次

    非静态内部类不能含有静态变量或静态方法

    非静态内部类依托于外部类对象而存在,即是内部类存在于外部类对象实例化之后!!!在外部类中

    static{ ......
    InnerClass.staticValue|staticMethod()
      ........}  error  ....  so
    static inner classes come into existence only in the context of an instance of the outer class.
    So ... if you're going to have a static method, the whole inner class has to be static. Without doing that, you couldn't guarantee that the inner class existed when you attempted to call the static method.
    The question to ask is -- if you do have a static method inside an inner class, how would you call that static method? The answer is, you can't.

    An inner class is tied to instances of the outer class.

    From Effective Java -- "Each instance of a nonstatic [nested] class is implicitly associated with an enclosing instance of its containing class".

    1、static类型的属性和方法,在类加载的时候就会存在于内存中。

    2、要使用某个类的static属性或者方法,那么这个类必须要加载到jvm中。

    3、非静态内部类并不随外部类一起加载,只有在实例化外部类之后才会加载。

    摘自https://blog.csdn.net/liu_shi_jun/article/details/74932666


    方法传递给匿名内部类或局部内部类的参数必须是final的(内部类中的参数是复制品,在其内改变不影响参数,为了迎合愚昧的。。。,强制内部类不允许修改)具体参考JAVA,匿名内部类,闭包

    https://blog.csdn.net/z69183787/article/details/68490440

    相关文章

      网友评论

          本文标题:内部类

          本文链接:https://www.haomeiwen.com/subject/kmlwsftx.html