0

Hey
I have a problem of how can i modify the bankAccount in order to count how many instances of the class have been created.

I think of that but the problem is that don't know how to add it to the original program :

private static int instanceCount = 0;
public CountTest()
{
instanceCount++;
}
pubic static int getinstanceCount()
{
return instanceCount;
}


Bank Account

public class BankAccount
{
	private int balance;

	public BankAccount(int initialBalance)
	{
		this.deposit( initialBalance );
	}
	
	public void withdraw( int amount)
	{
		balance = balance - amount;
	}
	
	public void deposit (int amount)
	{
		balance = balance + amount;
	}
	
	public int getBalance()
	{
		return balance;
	}
}


Thanks in advnace and i hope that someone can help me !

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by jwenting
0

if you just want to keep track of how many instances you create, add the static counter to the BankAccount class and increase it in the constructor.

But be aware that that will not keep track of the number of instances actually being accessible to the program, only the number of instances being created.
It also will not keep track of all instances being created anywhere at any time. It's limited to the scope of a single classloader.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.