This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with writing a remove() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with writing a remove() method" Watch "Help with writing a remove() method" New topic
Author

Help with writing a remove() method

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Hi,
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
  p2.remove();

  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
    PersonCount++;
  }
  
  // instance method to remove a person
  public void remove() {
    PersonCount--;
    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 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 ??

Cheers

Nathan
[ August 18, 2004: Message edited by: Nathan Russell ]
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Nathan,

This might not be what you want to hear, but I would do this a bit differently . Instead of tracking the number of people using a static variable etc I would create a collection of people which could be added/removed at will. Take a look at the java.util.Vector class for example. This allows you to store a list of objects (a bit like an array, but it is dynamically re-sized as you add elements to it) and it has add and remove methods. It also has a size method so you can see how many people objects are in there. Below is some pseudo code that might help...


[ August 18, 2004: Message edited by: Ben Wood ]

SCJP 1.4, www.gsi3d.org.uk
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Ben,

Thanks for the reply - your suggestion is actually what I've done already ! - almost line for line !

What I'm trying to do is more about experimenting, to see what is possible etc. Whilst the Person class etc that I have posted is real code that I have really written, its not a real world problem.

So, my static variable of PersonCount for the Person class was an 'easy' example, of my thinking that there might be an time where you want to track some kind of information about the objects of a given class - I chose the number of instances of that class as it was easy.

In a real world example, you would use an array, and arraylist or a vector as you have suggested - which is what I have done.

But, lets still persue the idea that you want to track some information about all of the Person instances you have created - lets say you have an instance variable for the Person class called DateLastModified, and you want to use it to track the date and time a person was last created or deleted - its a class variable rather than an instance variable as it is not specific to each instance of Person, its related to them all.

So, in the constructor, you would need a line that updated the DateLastModified class variable with a date stamp of now; and you would also need to do the same in a remove() method.

Can you see what I'm getting at ? Like I said, at this stage its more about experimenting to how how I might go about doing certain things, rather than trying to solve a real problem.

Cheers

Nathan
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
ah, in that case I suppose you might want to put the remove() method in the main code, not in the Person class, then you can set actual Person objects to null.



...something like that maybe?
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Hi Ben,

Thanks for your reply - sorry for the delay in me replying, just in the process of changing jobs !

Anyway, I've not tried your code, but I'm not sure it will work - will it ?

If the remove() method is in the main code, and I pass to it the Person object (p1) to remove; as part of the method, a new Person object (p) gets created that is local to the remove() method. Am I right in thinking that p and p1 are 2 different objects, pointing to the same instance (dont know if I'm using the right terminology here). And is it the p object that gets removed with the p=null; line. So, the original object of p1 still lives; but the PersonCount counter has been decremented.

Am I right in this thinking ?

Cheers

Nathan
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What you are trying here actually does have some real world applications. It is similar to reference counting. (Google for more details.)

Basically, Nathan is correct, although his terminology isn't quite correct. As he says, the above code won't work exactly as you want, unfortunately. Let me try to explain:

The trouble line is p = null;. This only sets the local reference to null. It does not change the reference that was passed into the method. To be more clear, if you did:

The somePerson reference still "points" to the Person object.

As a suggestion, you should look into overriding the finalize() method from the Object class. This method is called just before the garbage collector destroys the object. This seems to me like a good place to decrement your object count. You can look at the API docs for more information about finalize()

HTH

Layne


Java API Documentation
The Java Tutorial
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Nathan,

If you're interested in appropriate terminology, p and p1 are references [pointing] to the same object/instance.

Jules
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with writing a remove() method