删除内心阶级的反思

时间:2016-04-07 18:34:12

标签: java oop garbage-collection inner-classes finalize

我有两个类 - Temp1.java和State.java。 * Temp是一个跑步课。 *国家级包含一个额外的内部类 - 公民。

我没有创建对象的问题,但如果需要,我无法删除它们。

例如:我在Temp1类中创建State类的 israel usa 对象,以及c1,c2,c3,c4,c5和c6对象内心的公民阶级。创建这些对象后,我想删除c2和c5对象的引用。

我的问题: 如何使用GC和finalize()方法删除这些(c2和c5)对象?

谢谢。

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;
        }
    }
}

Delete two objects

0 个答案:

没有答案