-1

This is my first post so go easy on the formatting guys. Also, I'm very new to Java as well. I came into an error with my compiler with my program and I'm not sure how to fix it. I know I have to create an instance for my static function, but exactly how do I do that?

 import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;

    public class TestAccount{
    public static void main (String[] args) {
    Account account = new Account(1122, 20000);
    Account.setAnnualInterestRate(4.5);

    account.withdraw(2500);
    account.deposit(3000);
    System.out.println("Balance is " + account.getBalance());
    System.out.println("Monthly interest is " +
      account.getMonthlyInterest());
    System.out.println("This account was created at " +
      account.getDateCreated());
  }
}

class Account {




    private int id=0;
    private double balance = 0;
    private static double annualInterestRate = 0;
    private Date dateCreated;


    public Account()
    {

        this.id =0;
        this.balance =0;
        this.annualInterestRate =0;
        this. dateCreated = new Date();

    }

    public Account(int id, double balance)
    {

    this.id =id;
    this.balance =balance;
    this. dateCreated = new Date();
    }


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

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


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


    public double getBalance(){
    return this.balance;
    }


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


    public double getAnnualInterestrate() {
    return this.annualInterestRate;
    }

    public Date getDateCreated() {
    return this.dateCreated;
    }

    public double getMonthlyInterest() {
    return (this.annualInterestRate)/12;
    }

    public void withdraw(double amount) {

        this.balance -=amount;
    }


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

1 Answers1

0

One problem is here:

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

The keyword this refers to an object, but the method is static, so this method doesn't have access to individual objects. Remove the this keyword. I would also recommend reviewing the meaning of the static keyword.

Community
  • 1
  • 1
Ann Kilzer
  • 1,266
  • 3
  • 16
  • 39