3

I'm a bit confused

I created a class called person, that has an age and name attributes (and get set methods). Then in another class I want to create an array of persons , where each person has a different age and name. But some how in the end all of my persons end up with the last name and age. If I manually create them then it is ok, but with a for loop I've got that problem. What should I do to get different persons?

Here is the code of the person class:

public class person {
static String name;
static int age;
public person() {
    name="name";
    age=0;
}
public static String getName() {
    return name;
}
public static void setName(String name) {
    person.name = name;
}
public static int getAge() {
    return age;
}
public static void setAge(int age) {
    person.age = age;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

}

}

And here is the code where I want to create my array/matrix:

  public class array {
 static person[][] a;

 public static void main(String[] args) {
  a=new person[3][3];


  //manual created person
  person first=new person();
  person second=new person();
  person third=new person();
  first.setAge(12);
  first.setName("first");
  second.setAge(20);
  second.setName("second");
  third.setAge(40);
  third.setName("third");


  //automatic (here I get the disired effect)
  for (int i = 0; i < a.length; i++) {
   for (int j = 0; j < a.length; j++) {
    a[i][j]=new person();
    a[i][j].setAge(10+j);
    a[i][j].setName("Alia"+i);
    System.out.println(i+" "+j+" "+a[i][j].getName()+" "+a[i][j].getAge());
   }
  }

//  a[0][0]=first;
//  a[0][1]=second;
//  a[1][2]=third;
//  System.out.println(a[0][0].getName()+" "+a[0][0].getAge());

  //for checking , and it doesnt work anymore
  System.out.println(a[0][0].getName()+" "+a[0][0].getAge());

//  for (int i = 0; i < a.length; i++) {
//   for (int j = 0; j < a.length; j++) {
//    System.out.println(i+" "+j+" "+a[i][j].getName()+" "+a[i][j].getAge());
//   }
//   
//  }
  getname();

 }

 private static void getname() {
  System.err.println("get name function");
  for (int i = 0; i < a.length; i++) {
   for (int j = 0; j < a.length; j++) {
    System.out.println(a[i][j].getName());
   }
  }

 }

}

3 Answers 3

3

Remove the static keyword from the persons attributes. If it is static it is used by all instances (all person objects).

But I would do it like this:

public class Person {
    public final String name;
    public final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return name + " (" + age + ")";
    }

    public static void main(String... args) {
        List<Person> people = new LinkedList<Person>();
        people.add(new Person("David", 28));
        people.add(new Person("Andreas", 27));

        System.out.println(people);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, your attributes are declared static. A static attribute "belongs" to the class, not the instances, so all instances see the same String and int. You should be fine just removing the static from everything but main(). Then, new Person() will allocate new separate variables for everybody.

Comments

0

Problem is the static fields. Last values assigned to them would be reflected in all the object.

1 Comment

thank you everyone,I deleted static everywhere and it worked. I never knew about static and what it does. now my eyes are opened up a bit more. thanks again ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.