I would like to keep an arraylist of all the Member object references created in my program. Thus, I have placed a public static variable inside the Member class itself that gets added to in the constructor every time a new object is created. I have also placed an instance method called "deleteMember()" that will deleted the object reference if needed.
Is it good Java practice to keep this array inside of the Member class itself, and have additions made in the constructor and deletions through an instance method?
The other option would be to put the arraylist inside the main() function and use the arraylist class functions to add and delete when needed.
Code:
public class Member {
private String fullName;
private int birthYear;
private double weight;
private boolean isAdmin;
public static ArrayList<Member> memberList = new ArrayList<Member>();
public Member(String fullName, int birthYear, double weight, boolean isAdmin) {
//super();
this.fullName = fullName;
this.birthYear = birthYear;
this.weight = weight;
this.isAdmin = isAdmin;
//add the newest object to the memberList arraylist
memberList.add(this);
}
public void deleteMember()
{
//delete current object from the memberList arraylist
memberList.remove(this);
}
}