2

I am trying to create an array of Person (a class that with variables String name, and double total). But for some reason, creating a second Person replaces(?) the first person. . .

Person[] p = new Person[40];
    
p[0] = new Person("Jango", 32);
p[1] = new Person("Grace", 455);
    
System.out.println( p[0].getName() );
System.out.println( p[1].getName() );
System.out.println( p[0].equals(p[1]) );

The output is:

Grace
Grace
false

Why isn't it:

Jango
Grace
false

????????????

public class Person {

    @SuppressWarnings("unused")
    private Person next;
    private String name;
    private double total;

    public Person(String _name)
    {
        name = _name;
        total = 0.0;
        next = null;
    }

    public Person(String _name, double _total)
    {
        name = _name;
        total = _total;
        next = null;
    }

    public String getName()
    {
        return name;
    }
}
7
  • 6
    can you show us the person class? maybe you use a static variable to store the name? Commented Dec 21, 2012 at 21:19
  • Can we see the Person class? Commented Dec 21, 2012 at 21:19
  • Can you show us the constructor of Person? Commented Dec 21, 2012 at 21:19
  • Assuming a sane definition of Person, this shouldn't happen. Either you typo'd one of the array indices in your actual code, or the error is in Person. Commented Dec 21, 2012 at 21:20
  • 1
    You need to look up what "static" means in a class field, and also how object comparison works in Java. Commented Dec 21, 2012 at 21:21

3 Answers 3

6

Your problem is that the name instance variable is declared as static, making it a class variable. Any change to name will be changed for every instance of that class.. You need to remove the static identifier from name and from total and your code will work fine.

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

4 Comments

If they are static they are not instance variables. So your statement is contradictory.
Well, name would be an instance variable for a Person class.. and that is what he is trying to do.. That is why I used the term.
If they are static they are class variables, not instance variables. Arent't they?
You are correct... And I am aware. I was trying to say that he declared instance variables as static... making them class variables. which is not what he intended to do. Sorry for not typing it all out..
3

Currently these variables are static which means that they they will retain the last values assigned.

private static String name;
private static double total;

You need to make these fields class instance variables:

private String name;
private double total;

See Understanding Instance and Class Members

Comments

1

Your fields are static. They should not be, if you want them to be able to store a separate instance of a name and total for each instance of the class.

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.