Will passing outer object class object to static inner creates a memory leak?
And how I can check whether it will create the memory leak?
Below is what I am looking for where I have created the inner and outer class, and the way I am planning to use and pass the object.
My main doubt is because I think at some point even though OuterClass object would no more be referenced from anywhere still it would not be GC'ed because InnerClass (which is a STATIC class) is holding a reference of the it. So, I feel this would cause a memory leak.
public class OuterClass {
private int id;
private String name;
public static class InnerClass{
OuterClass outerClass;
public InnerClass(OuterClass outerClass) {
this.outerClass = outerClass;
}
public void printOuterClassDetails(){
System.out.println(outerClass.getId() + " | " + outerClass.getName());
}
}
public OuterClass(int i, String string) {
this.id = i;
this.name = string;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
OuterClass outerClass = new OuterClass(1, "A");
OuterClass.InnerClass innerClass = new InnerClass(outerClass);
innerClass.printOuterClassDetails();
}
}