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 static String name;
private static 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;
}
}
Person
, this shouldn't happen. Either you typo'd one of the array indices in your actual code, or the error is inPerson
. – delnan Dec 21 '12 at 21:20