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 (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 ??

Cheers

Nathan