I have a class called Person which is like the following:
public class Person
{
public static String name;
public static int regNumber;
public String getName() {
return name;
}
public int getRegNumber() {
return regNumber;
}
public Person(int x) {
this.name = n;
this.regNumber = x;
}
}
I use this class to fill an array with students:
public class PeopleArray
{
public static void main(String[]args) {
Person [] students = new Person[3];
int reg = 1;
for (int i = 0; i<students.length; i++) {
students[i] = new Person(reg++);
}
for (Person stu: students) {
System.out.println(stu.getRegNumber());
}
}
}
The problem is that when I try to print out the ages of each individual person it seem that the array was filled with only the last object created by the Person class, because the only number that is printed is the number 2.
What am I doing wrong?
staticmodifier from your fields. And look up its meaning.this.name = n;?