Possible Duplicate:
ClassnotFound exception using java reflection
I get exception, java.lang.ClassNotFoundException: SavingAccount, while executing the following code:
public class AccountFactory {
public void getAccount()
{
IAccount account;
account = null;
try
{
account = (IAccount)Class.forName("SavingAccount").newInstance();
account.Deposit(500);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
What could be the possible reasons causing the error?
this is my saving account code:
public class SavingAccount implements IAccount {
int money;
SavingAccount()
{
money =0;
}
public void WithDraw(int m)
{
money--;
}
public void Deposit(int m)
{
money++;
System.out.println(money);
}
}
(IAccount)Class.forName("SavingAccount").newInstance()? Why not writenew SavingAccount()instead?