0

I have a linkedlist of Accounts, containing Employees and Managers (inheriting from Account). The problem is I have noticed the last added item seems to be overwritting the rest in the list. Why is it doing this? what I am doing wrong? thanks. I'll put my code below and console output. Sorry in advance if i am being really stupid and missing something obvious!

public class Database {
static List <Account> Accounts = new LinkedList<Account>();

public static void main(String[] args) {
        Employee Geoff = new Employee("Geoff", "password1");
        Manager Bob = new Manager("Bob", "password2");
        Employee John = new Employee("John", "password3");

        Accounts.add(Geoff);
        Accounts.add(Bob);
        Accounts.add(John);
        list();
    }

public static void list() {
    for (Account u : Accounts) {
            System.out.println(u);
    }
}

Console Output is:

John, John, John 

:(

Edit: code has been changed sorry guys!

public abstract class Account {

    protected static String name;
    protected static String passcode;


    public User(String name, String passcode) {
        this.name = name;
        this.passcode = passcode;
    }
}

Both manager and employee inherit from this so for manager:

public Manager(String name, String passcode) {
    super(name, passcode);

}
10
  • what is list? do you mean menu? could you trim the code to the minimum and show the full program? Commented Mar 3, 2015 at 16:41
  • What is Users? What is list()? Commented Mar 3, 2015 at 16:41
  • Try posting some code that actually compiles. What does the list() method do? When is menu() called? Commented Mar 3, 2015 at 16:41
  • Sorry guys!, I posted the wrong method, please take another look Commented Mar 3, 2015 at 16:47
  • 5
    just question, by any chance is your account name field is static? Commented Mar 3, 2015 at 16:50

2 Answers 2

4

Remove key word `static from declaration of fields and it will work fine.

static variables are associated with the class, not with object. Which means those fields are shared between each instance of this class.

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

Comments

0

Class variables (static) will only have a single instance that is shared between all instanciations of the class. That means that every time you say "this.name", it is semantically equivalent to saying "User.name", since "this" refers to the instance, not the class.

Change the class variables (static variables) to instance variables (non static variables) and everything will work as you expect.

Here is the documentation that explains class vs instance variables.

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

2 Comments

Thank you :), so if I wanted to compare if a certain name is contained in the lost I should avoid static variables altogether as well?
Yes you should. Make sure you understand exactly what static does before you use it in a program, otherwise you will get confusing behaviour. The book Effective Java (amazon.ca/Effective-Java-Edition-Joshua-Bloch/dp/0321356683) is a really good read and covers all this stuff and the best practices very well.

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.