I was wondering how a nested class works in a for loop:
- will the object of the class be destroyed after every for interation?
- will the instance of the class be destroyed automatically by "garbage"?
- once the for loop finishes will the object from the nested class persist in memory? can it be recalled from other places in the program?
This is the code:
class Outer {
int outer_x = 100;
void test() {
for(int i=0; i<10; i++) {
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
Inner inner = new Inner();
inner.display();
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}
Class
instance ofInner
. – Flaviaflavian