0

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

Delete two objects

Cœur
  • 37,241
  • 25
  • 195
  • 267
morris
  • 131
  • 1
  • 8
  • Each citizen has an implicit reference to its state. A state will be eligible to GC once it's not reachable anymore. Since all the citizen references are still reachable, their state is reachable, too. You don't delete objects in Java. You just let the GC collect them. – JB Nizet Apr 07 '16 at 18:41
  • if I run c2 = null, and System.gc() in my running class I get an exception - Exception in thread "main" java.lang.NullPointerException at d.nestedClasses.inner.Test1.main(Test1.java:61). – morris Apr 08 '16 at 10:51
  • 1
    Yes, calling a method on a null reference always throws a NullPointerException. That has nothing to do with GC. And you should never call System.gc(). – JB Nizet Apr 08 '16 at 11:22

0 Answers0