0

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());
        }

    }

}

6 Answers 6

10

Don't make name and age static. Also, make all of your methods non-static.

Static variables are specific to the class. Instance variables are specific to each object of that class.

Then, when inside a class, instead of referring to a variable as Person.name, just refer to it as name

Sign up to request clarification or add additional context in comments.

2 Comments

Amen! And the reason why this is a problem is that by making the variables static, you make them variables of the class, not the object, and all objects will then share the same name and age variable -- so if you change it for one, you change it for all.
There are some situations where you should use static variables. An example in this case is if he wanted to keep track of how many people there were. But most things should generally not be static (in my experience)
4

static variables are class-wide, this means that every instance of Person shares the same static variables.

Comments

0
public class Person {

private String name;
private int age;

public Person(){
    name = "NAME";
    age = 0;
}    

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

}

public String getName(){
    return name;
}

public int getScore(){
    return age;
}

public String printString(){

    return name + " " + age;
}

}

don't use static vars and methods when you are using them for objects

Comments

0

when you have:

class aClass {
   private static String name;
}

EVERY instance(things that get made with new) of that class that is created shares the SAME 'name' variable. When you do this...

class aClass {
   private String name ; 
}

Each instance will have its own separate name.

Comments

0

Don't make name and age static.

When a field is static, it means, that is not owned by the instances of this class, but by the class itself. This means that there is only one variable name in all your application, and all Person instances will share it. so when you change the name and the age, you change for all classes, and they all will have the last ones you used.

Comments

0

Which works, the main problem is that the array only saves the last entry that I input. How can I fix that?

The problem behind this is you are using static variables, instead, they should be instance variables. Since they are static, there is a single copy of them which is getting overwritten everytime and in the end the last pair of name and age is what you get.

Making them instance variables should solve your problem.

Comments

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.