Alex A
Alex A

Reputation: 11

basic programming keeping track of objects java

public Person (String name, int age, boolean isFemale)

In a program I am writing I have to keep track of how many Person objects are created. I also have to make static variables for the combined ages of all the Persons as well as how many of them are female. If someone could at least point me in the right direction thatd be awesome I have no idea how to do this step.

Upvotes: 0

Views: 592

Answers (1)

user987339
user987339

Reputation: 10717

You have to add some static fields to your class, that will be updated in constructor when new Person object is created. static fields are common for all instances of a same class.

For example:

private static personCount;
private static femaleCount;
public Person (String name, int age, boolean isFemale){
    personCount++
    ...
}

Fill ... with missing code for counting females.

Upvotes: 2

Related Questions