Basically I have a variable, zlort = one;
I want to concatenate the value of zlort into a variable (object reference) name.
Like
BankAccount Accountzlort = new BankAccount;
I want the zlort in Account.zlort to actually be the replaced with value of zlort (one–meaning I want the value to be Accountone), and not zlort itself.
Is it possible to do this?
Thanks!
After reading the explanations in your comments, the fact that you can’t use an array but can use an `ArrayList’…
Rather than creating a new variable name (or array element, or map value) for each
BankAccount, you can probably use scope to your advantage.Scope is the concept that a reference to a variable only has meaning within a certain part of code. If you declare a variable inside a method, that variable can only be seen within that method. A variable declared within a block (a loop, if statement, etc ) can only be seen from within that block.
Class fields have a different kind of scoping that can be adjusted with keywords (see here).
For example:
SO! If you create a
BankAccountobject within the loop, you don’t have to worry about creating a new name for the next one. Each time the loop iterates it will become a new object (when you create it).If you have to store it, you definitely will need to use an array or other data structure (
ArrayList!).Building on the idea of scope, you -can- have the same variable name for each new
BankAccount. A variable reference name isn’t guaranteed to be paired with the object that it refers to. That is a convenience to the programmer, so you don’t have to know the exact memory address it is being stored in.For example:
The new
Objectcreated in the loop does not belong to ‘obj’. You as a programmer use ‘obj’ to point to theObject. The program doesn’t really know what obj means, other than the fact that it points to the Object you justcreated.Finally, you can use this along with an
ArrayListto make your life easier.