Jeel Shah
Jeel Shah

Reputation: 3324

Java classes problem

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

    }

}

Upvotes: 0

Views: 161

Answers (6)

Sachin
Sachin

Reputation: 18747

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.

Upvotes: 0

lpinto.eu
lpinto.eu

Reputation: 2127

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.

Upvotes: 0

Andrew
Andrew

Reputation: 2568

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.

Upvotes: 0

ratchet freak
ratchet freak

Reputation: 48186

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

Upvotes: 0

jjwchoy
jjwchoy

Reputation: 1908

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

Upvotes: 4

matzahboy
matzahboy

Reputation: 3024

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

Upvotes: 10

Related Questions