1

I just want to make sure I'm doing the right thing. I have a subclass Account and then two subclasses SavingsAccount and CreditAccount and i want to store them in an arraylist, but when I do like this I get errors

List<Account> accountList = new ArrayList<Account>();
// creditaccount
Account account = new CreditAccount();
list.add(account);

// savingsaccount
Account account = new SavingsAccount();
list.add(account);

I thought I could just add them like that, but I guess there must be a unique name like this...

Account account1 = new SavingsAccount();
list.add(account1);

...or am I misunderstanding it? I'm new to this, so help i preciated! Thanks!

2 Answers 2

5

You're correct, variable names are unique in its given scope (local in-method vs. instance variable). Nevertheless, as this is object-oriented you can reuse the variable as it only references a given object:

List<Account> accountList = new ArrayList<Account>();
// creditaccount
Account account = new CreditAccount();
list.add(account); // <-- adds the creditAccount to the list

// savingsaccount
account = new SavingsAccount(); // <-- reuse account
list.add(account); // <-- adds the savingsAccount to the list

Personally, I do not like that approach and rather use self-explanatory names like:

Account creditAccount = new CreditAccount();
Account savingsAccount = new SavingsAccount();
...
list.add(creditAccount);
list.add(savingsAccount);

UPDATE 1: If you do not have to further initialize the account objects you could just do this:

list.add(new CreditAccount());
list.add(new SavingsAccount());

UPDATE 2: I forgot to mention that there is a 'more advanced' approach using anonymous inner blocks, enabling you to declare a variable more than once inside a method:

void someMehtod() {
    List<Account> accountList = new ArrayList<Account>();

    { // <-- start inner block
        Account account = new CreditAccount();
        accountList.add(account);
    } // <-- end inner block

    {
        Account account = new SavingsAccount();
        accountList.add(account);
    }

    account.toString() // compile time error as account is not visible!

}
6
  • So if I first declare like this: Account account; and then: account = new CreditAccount(); and account = new SavingsAccount(); I can use the same variable account
    – 3D-kreativ
    Jan 2, 2012 at 9:37
  • Reply to your last edit: Then I have to make a declaration for each variable to account like: Account creditAccount; and Account savingsAccount; ?
    – 3D-kreativ
    Jan 2, 2012 at 9:38
  • Yes, but you must work with the former account object, otherwise you will override the reference when assigning a new object. If you're not familar with this approach or object-orientation in general, I recommend to do some simple tests to understand the concept by e.g. using a class like java.lang.String. (answer to your first comment).
    – home
    Jan 2, 2012 at 9:40
  • Second comment: yes, you need two declarations. It all depends on your application an whether you have to further initialize the objects (see my update).
    – home
    Jan 2, 2012 at 9:42
  • Hmm, I guess I only need to initialize the account object only once and then add several creditAccount and savingsAccount into the list like you just wrote in your last update?
    – 3D-kreativ
    Jan 2, 2012 at 9:49
3

Account account = new SavingsAccount();
This will give a compile time error. You cannot declare account twice. Change above statement to account = new SavingsAccount();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.