Java objects problem?

i wanted to test out objects, so... naturally i made an object, first i made a class called person , i gave it a bunch of methods to set the info and retrieve it, then i made a class called "PEOPLE" the people class is a bunch of objects from the person class, i dont know what i did wrong, here is the "person" class

public class Person{

private static int age;

private static double money;

private static String name;

private static String gender;

public Person(String NAME){

name=NAME;

}

public void setAge(int AGE){

age=AGE;

}

public void setGender(String GENDER){

gender=GENDER;

}

public void setMoney(double MONEY){

money=MONEY;

}

public String getName(){

return name;

}

public int getAge(){

return age;

}

public String getGender(){

return gender;

}

public double getMoney(){

return money;

}

public void printInfo(){

System.out.println(name+" is "+age+" year old "+gender+", with $"+money+".");

}

}//end class

and here is the "People" class

public class people{

public static void main(String[] args){

Person p1 = new Person("Max");

p1.setAge(17);

p1.setGender("male");

p1.setMoney(1004.29);

Person p2 = new Person("Oliver");

p2.setAge(17);

p2.setGender("male");

p2.setMoney(2824.47);

Person p3 = new Person("Michelle");

p3.setAge(17);

p3.setGender("female");

p3.setMoney(11204.45);

p1.printInfo();

p2.printInfo();

p3.printInfo();

}

}

when i started with one person, (max) it worked great, i add 2 more people, now it is just printing the info on michelle 3 times. HELP

2 Answers

Relevance
  • Bob
    Lv 7
    6 years ago
    Favorite Answer

    That's because you've marked the fields static!

    Static fields are shared by ALL objects of the class, so if you set a static field for one object, it is set to the same for all objects.

    Remove the statics and all should be well

    PS. In your setter methods, don't write the parameter name in CAPITAL letters, because a name in all caps is used for a constant value. The approved way to do this is use "this." to access the fields.

    Eg:

    public Person(String name){

    this.name=name;

    }

    An alternative approach, not as good, is to append "in" to the parameter name:

    public Person(String inName){

    name=inName;

    }

    • Commenter avatarLogin to reply the answers
  • John
    Lv 7
    6 years ago

    /*

    static is variable for "class";

    p1, p2 and p3 are objects instance of "class";

    the "static int age and other variables" are accessable from all objects means the variables keep storing the last value only;

    */

    import java.util.*;

    import java.text.*;

    public class People {

    public static void main(String[] args) {

    Person[] persons = new Person[2];

    persons[0] = new Person("Max");

    persons[0].setMoney(1004.29);

    persons[0].setSex(Person.Gender.MALE);

    persons[0].setDateOfBirth("1997-02-15");

    // this program was generated on 2014-02-16

    persons[1] = new Person("Oliver");

    persons[1].setMoney(2824.47);

    persons[1].setSex(Person.Gender.FEMALE);

    persons[1].setDateOfBirth("1997-02-17");

    for (Person p : persons) {

    System.out.println(p);

    }

    System.exit(0);

    }

    }

    class Person {

    enum Gender {

    MALE, FEMALE

    }

    private Date dateOfBirth;

    private double money;

    private String name;

    private Gender sex;

    public Person(String name) {

    this.name = name;

    }

    public void setMoney(double money) {

    this.money = money;

    }

    public void setSex(Gender sex) {

    this.sex = sex;

    }

    public void setDateOfBirth(String dateString) {

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    try {

    this.dateOfBirth = df.parse(dateString);

    } catch (ParseException e) {

    }

    }

    public int getAge() {

    return calculateAge(this.dateOfBirth);

    }

    @Override

    public String toString() {

    return String.format("%s is %d year old %s with %,.2f.",

    name, getAge(), this.sex, this.money);

    }

    /**

    * Calculate age, it should be in common class

    * @param dateOfBirth date of birth

    * @return integer age

    */

    private int calculateAge(Date dateOfBirth) {

    Calendar dob = GregorianCalendar.getInstance();

    dob.setTime(dateOfBirth);

    Calendar today = GregorianCalendar.getInstance();

    int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

    if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH)) {

    age--;

    } else

    if (today.get(Calendar.MONTH) == dob.get(Calendar.MONTH)

    && today.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH)) {

    age--;

    }

    return age;

    }

    }

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.