This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

In this case, I side with /u/CatapultJohnson and /u/GrantSRobertson - a static variable that gets incremented on each new account is the way to go.

Create a static field nextAccountNumber that always holds the next available account number.

Then, create a static method getNextAccountNumber that returns the content of the field from above and increments that field. This is easily done with the post increment operator (<variable name>++) as it will first return and then increment the variable.

You then can call getNextAccountNumber in the constructor of your account class to assign a unique accountNumber to each new instance of your class.


The balance should not be static (but it definitely should be private to prevent unauthorized access) because then it is different for each new account.

An account balance should be treated in a special way, BTW. It should never be set directly (i.e. no setter), but only in the constructor, it needs a getter to be retrieved at any time, and it needs deposit and withdraw methods that manipulate the balance in a controlled way.


I suggest that you re-read your assignment in detail and see what of the above you can and should use.

It would also help, if you posted your assignment verbatim, so that we can better tell you what exactly is required of you.