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!

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

6 Comments

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
Reply to your last edit: Then I have to make a declaration for each variable to account like: Account creditAccount; and Account savingsAccount; ?
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).
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).
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?
|
3

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

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.