I have a problem with classes. I made my own class called "Person". Which takes the parameters "name" and "age". In my other class i want to make a Person[]. Which works, the main problem is that the array only saves the last entry that I input. How can I fix that?
Please note: This is not homework. I am working on my own project.
code:
public class Person {
private static String name;
private static int age;
public Person(){
Person.name = "NAME";
Person.age = 0;
}
public Diver(String name,int age){
Person.name = name;
Person.age = age;
}
public static String getName(){
return Person.name;
}
public static int getScore(){
return Person.age;
}
public static String printString(){
return Person.name + " " + Person.age;
}
}
public class TESTER {
public static void main(String[]args){
Person[] persons = new Person[4];
persons[0] = new Person("bob2",15);
persons[1] = new Person("bob1",15);
persons[2] = new Person("bob",16);
for(int i = 0;i<persons.length;i++){
System.out.println(persons[i].printString());
}
}
}