DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004

    Help with remove method

    I'm new to Java, and OO programming in general - I'm getting there though, its not so bad
    But I need some help writing a method within my class that removes the instance in question - I guess the inverse of a constructor method.

    So, my main code might look something like:

    // create my new Person objects
    Person p1 = new Person("Nathan","M",33);
    Person p2 = new Person("Louise","F",33);
    Person p3 = new Person("Jake","M",3);

    System.out.println ("There are " + Person.getCount() + " Person objects");
    // some other code to manipulate these objects
    // ....
    // ....
    // ....

    // now remove Person object p2

    System.out.println ("There are now " + Person.getCount() + " Person objects");

    And then my Person class might look like:

    public class Person {

    // Class variables
    private static int PersonCount = 0;

    // Instance variables
    private String PersonName = "";
    private String PersonSex = "";
    private int PersonAge = 0;

    // constructor for a new person
    Person (String newName, String newSex, int newAge) {
    PersonName = newName;
    PersonSex = newSex;
    PersonAge = newAge;
    // Increment the number of people records

    // instance method to remove a person
    public void remove() {
    this = null;

    // Class method to return the number of people defined
    public static int getCount() {
    return PersonCount;

    // instance method to return the person name
    public String getName() {
    return PersonName;

    // instance method to return the person age
    public int getAge() {
    return PersonAge;

    // instance method to return the person sex
    public String getSex() {
    return PersonSex;


    I really want to maintain the PersonCount static variable for the class; so that I know how many Person objects have been defined - hence incrementing the variable in the constructor, and decrementing it in the remove() method.

    However, the code does not compile, failing on the this = null; line (in blue above)
    I guess I can understand why this is - I'm in the context of an object, running its method, which is trying to remove itself !
    But, there must be a way of doing this ????

    I guess in my main code I could do p2=null; ??? - I've not tried, as it wont acheive the objective of decrementing the PersonCount variable.

    Any ideas ??



  2. #2
    Join Date
    Feb 2004
    if you take a look at the finalize() method in Object you will see that this method is called whenever the garbage collector destroys an object. Since all objects inherit Object your object will have this method. You simply need to override it and make it decrement the variable that's counting instances. Of course this method will only be run when the garbage collector destroys the object, and this only happens when ALL references to the object are dead.

  3. #3
    Join Date
    Aug 2004
    Hi Mike,
    Thanks for you reply. OK, so I can write my finalize method for the Person class as:

    public void finalize() {

    But how, from my main code, do I actually delete / remove any given Person instance ?

    Also, I am led to understand that you cannot guarantee when the garbage collector will run, and consequently when your finalize() method will run. Therefore, assuming the code within the finalize() method is critical to the rest of the code, its not going to work because you do not know when your finalize() method will run in relation to removing/deleting the object.
    What you want is to do it in one method / chunk of code - thats why I was trying to write a remove() method.

    Or am I wrong on this ?

  4. #4
    Join Date
    May 2004
    You're better off keeping all your persons in a collection and removing the actual references to each Person from the collection. Your person count will always be the size of the collection, so you will not have to worry about incrementing/decrementing a static counter variable.

      public static void main(String[] args)
        java.util.ArrayList people = new java.util.ArrayList();
        people.add(new Person("Nathan","M",33));
        people.add(new Person("Louise","F",33));
        people.add(new Person("Jake","M",3));
        System.out.println("There are " + people.size() + " Person objects");
        System.out.println("There are " + people.size() + " Person objects");
    edit: Although, you might want to make a containing class such as "Room" that would "hold" your Persons and it would be responsible for removing people. Or perhaps the people would know which room they were in, and THEY would remove themselves from the room. There are many ways to do it

  5. #5
    Join Date
    Feb 2004
    The garbage collector will collect memory when there are no reachable references to a particular object. If you make a reference (ie the variable name you use in your code) = null, then that removes the reference from the object. Think of the reference names you use in the code as strings with a label on the end. When you create an object java gives it a space in memory, then attaches your label to it. By making a reference equal null you cut this string. When all strings to an object are cut, the object will be collected by the garbage collector.

    You can force a call to the garbage collector using System.gc() (i think), so if you make a variable = null then call the garbage collector it should run the finalize method on that object IF that was the only variable to it. If one object is shared among several different classes then making one of them null won't sever all the references to it, so it won't be garbage collected.

    Having said all this, Drains idea is good. You may prefer to do it that way. I don't know exactly what the count variable is for though, so i'll leave it up to you to determine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.