非静态内部类不能含有静态变量或静态方法
非静态内部类依托于外部类对象而存在,即是内部类存在于外部类对象实例化之后!!!在外部类中
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
网友评论