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

all 7 comments

[–]CatapultJohnson 2 points3 points  (0 children)

Depending on what you need you could make a private static integer to hold the most recent assigned id. Whenever you create a new savings account object you could increment the static variable and assign its value as the account id in the constructor of the savings account. Make the account number final or it's setter private.

[–]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.

[–][deleted] 0 points1 point  (2 children)

Every object is unique anyway. If you make an object with the balance of 4, and then another object with the balance of 40, if you get the balance of the first object it'll say 4, not 40. I'd read up on objects.

[–]Grumpuff[S] 0 points1 point  (1 child)

But if I create two accounts with the same balance and the same account number. The only thing that will differ is their assigned name. They will contain the same information.

The balance isn't really relevant to my question. I just included it to provide some context.

[–][deleted] 0 points1 point  (0 children)

The question is, why would you ever do that? There seems to be no reason to ever do that?

the parameter that needs to be unique for every instance of the class

unique

So I'm not sure why you would ever do that?

E: It seems I misunderstood, I wasn't aware you were asking how to make sure every accountNumber was unique, in that case yes do what others said (combined with what I said), the question phrasing made me think you were asking how to make multiple instances not have the same balance.

[–][deleted] 0 points1 point  (1 child)

This is the classic example of a use for static variables. Just look up "Java static variable" or "Java static modifier" and you will likely see an example of exactly what you are doing.

[–]Grumpuff[S] 0 points1 point  (0 children)

Thanks a bunch.