我声明了一个静态内部类,我在外部类的方法中创建了一个新实例。但是,我得到的结果表明在我的方法中每次都使用相同的 Inner 类实例。下面的例子,
public class Outer{
public method m(){
Inner n = new Inner(); //Creating local instance of Nested class
n.something();
}
public static class Inner{
Map<K,V> cache = new Map<K,V>();
void something(){
//use and update cache;
}
}
}
public Test{
public static void main(String a[]){
Outer o = new Outer();
o.m();
o.m(); //cache was still available
}
}
有人可以帮助解释为什么没有创建内部类的两个实例吗?
另外,如果我static
从内部类中删除,这种行为会改变吗?