I have two classes - Temp1.java and State.java . * Temp is a running class. * The State class contained an additional inner class - Citizen.
I haven't the problem for create the objects, but I can't remove them if I need that.
For example: I create in the Temp1 class the israel and usa objects of the State class, and c1, c2, c3, c4, c5 and c6 objects of the inner Citizen class. After creating these objects I want to delete the reference of c2 and c5 objects.
My question: How can use with GC and finalize() method for removing these (c2 and c5) objects?
Test1.java:
package d.nestedClasses.inner;
import d.nestedClasses.inner.State.Citizen;
public class Test1 {
public static void main(String[] args) {
State israel = new State("Israel", 8000000);
State usa = new State("USA", 120000000);
/////////////////////////////////////////////////////////////
Citizen c1 = usa.new Citizen("John", 1);
Citizen c2;
c2 = usa.new Citizen("Mark", 2);
Citizen c3;
c3 = usa.new Citizen("Judith", 3);
/////////////////////////////////////////////////////////////
Citizen c4;
c4 = israel.new Citizen("David", 1);
Citizen c5;
c5 = israel.new Citizen("Moshe", 2);
Citizen c6 = israel.new Citizen("Oren", 3);
/////////////////////////////////////////////////////////////
System.out.println(c5.getName());
System.out.println(c5.getState());
System.out.println(c5.getStateName());
System.out.println("-----------------------------------------");
System.out.println(c1.getName());
System.out.println(c1.getState());
System.out.println(c1.getState().getName());
System.out.println("-----------------------------------------");
usa = null;
/*
c1 = null;
c2 = null;
c3 = null;
*/
usa = c1.getState();
// System.out.println(usa);
System.out.println("=========================================");
System.out.println("c1# " + c1.getName() + " : " + c1.getStateName());
System.out.println("c2# " + c2.getName() + " : " + c2.getStateName());
System.out.println("c3# " + c3.getName() + " : " + c3.getStateName());
System.out.println("-----------------------------------------");
System.out.println("c4# " + c4.getName() + " : " + c4.getStateName());
System.out.println("c5# " + c5.getName() + " : " + c5.getStateName());
System.out.println("c6# " + c6.getName() + " : " + c6.getStateName());
}
}
State.java:
package d.nestedClasses.inner;
public class State {
// attributes ---------------------------------------------------
private final String name;
private int population;
// CTOR ---------------------------------------------------------
public State(String name, int population) {
super();
this.name = name;
this.population = population;
}
// get, set -----------------------------------------------------
public String getName() {
return name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
// methods ------------------------------------------------------
@Override
public String toString() {
return "State [name=" + name + ", population=" + population + "]";
}
// inner class --------------------------------------------------
public class Citizen {
// attributes ---------------------------------------------------
private String name;
private int id;
// CTOR ---------------------------------------------------------
public Citizen(String name, int id) {
super();
this.name = name;
this.id = id;
}
// get, set -----------------------------------------------------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
// methods ------------------------------------------------------
public State getState() {
return State.this;
}
public String getStateName() {
return State.this.name;
}
}
}