2 Replies - 1021 Views - Last Post: 26 January 2011 - 03:34 PM Rate Topic: -----

#1 lina29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-September 10

Non-static method error

Posted 26 January 2011 - 03:13 PM

I'm trying to write a bank account that allows the user to withdraw money from their checking account. However, I keep getting the error (non-static method withdraw(double) cannot be referenced from a static context). I'm not allowed to modify the superclass account. Any help would be greatly appreciated!

Super Class Account
class Account {
  private int id;
  private double balance;
  private double annualInterestRate;
  private java.util.Date dateCreated;

  public Account() {
    dateCreated = new java.util.Date();
  }

  public Account(int id, double balance, double annualInterestRate) {
    this.id = id;
    this.balance = balance;
    this.annualInterestRate = annualInterestRate;
    dateCreated = new java.util.Date();
  }

  public int getId() {
    return this.id;
  }

  public double getBalance() {
    return balance;
  }

  public double getAnnualInterestRate() {
    return annualInterestRate;
  }

  public void setId(int id) {
    this.id =id;
  }

  public void setBalance(double balance) {
    this.balance = balance;
  }

  public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
  }

  public double getMonthlyInterest() {
    return balance * (annualInterestRate / 1200);
  }

  public java.util.Date getDateCreated() {
    return dateCreated;
  }

  public void withdraw(double amount) {
    balance -= amount;
  }

  public void deposit(double amount) {
    balance += amount;
  }
}





Checking Account


import java.util.Scanner;

public class CheckingAccount extends Account
{
	private static double balance;
	public static void main(String[] args)
   {
   	Scanner input = new Scanner(System.in);
	System.out.print("What is the amount of money you want to withdraw?: $");
	double amount=input.nextDouble();
	withdraw(amount);	
   }
	
	public CheckingAccount(int id, double balance, double annualInterestRate) 
 {
 	super(id, balance, annualInterestRate);
    id=1234;;
    balance=5000;
 }
  
   public void withdraw(double amount) 
  {
    balance -= amount;
    if (balance<0)
    	balance-=100;
  }
   
}


Is This A Good Question/Topic? 0
  • +

Replies To: Non-static method error

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Non-static method error

Posted 26 January 2011 - 03:30 PM

Instantiate a new CheckingAccount in main() and invoke withdraw() from that Object. Don't invoke withdraw() directly in main().
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Non-static method error

Posted 26 January 2011 - 03:34 PM

main() is a static method, and thus, you can't call a non-static method for obvious reasons. However, if you make a CheckingAccount object and call withdraw on that, it will work just fine!

CheckingAccount ca = new CheckingAccount();
ca.withdraw(amount);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1