Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot make a static reference to the non-static field

Tags:

java

I apologize ahead of time if this code isn't formatted correctly, trying to paste instead of retyping each line. If it isn't right, can someone tell me an easy way to paste multiple lines of code at once?

My main question is that I keep getting an error message stating: Cannot make a static reference to the non-static field balance.

I have tried making the methods static, with no result, and making the main method non-static by removing "static" from the header, but then I get the message: java.lang.NoSuchMethodError: main Exception in thread "main"

Does anyone have any ideas? Any help is appreciated.

public class Account {

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

        account.withdraw(balance, 2500);
        account.deposit(balance, 3000);
        System.out.println("Balance is " + account.getBalance());
        System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
        System.out.println("The account was created " + account.getDateCreated());
    }

    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    public java.util.Date dateCreated;

    public Account() {
    }

    public Account(int id, double balance, double annualInterestRate) {
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    public void setId(int i) {
        id = i;
    }

    public int getID() {
        return id;
    }

    public void setBalance(double b){
        balance = b;
    }

    public double getBalance() {
        return balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double interest) {
        annualInterestRate = interest;
    }

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

    public void setDateCreated(java.util.Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public static double withdraw(double balance, double withdrawAmount) {
        double newBalance = balance - withdrawAmount;
        return newBalance;
    }

    public static double deposit(double balance, double depositAmount) {
        double newBalance = balance + depositAmount;
        return newBalance;
    }   
}
like image 241
jshield Avatar asked Nov 12 '11 00:11

jshield


People also ask

Can not make static reference to non-static field?

i.e. referring a variable using static reference implies to referring using the class name. But, to access instance variables it is a must to create an object, these are not available in the memory, before instantiation. Therefore, you cannot make static reference to non-static fields(variables) in Java.

Why can't static method access non-static fields?

Non-static variables are part of the objects themselves. To use a non-static variable, you need to specify which instance of the class the variable belongs to. ... In other words, non-static data cannot be used in static methods because there is no well-defined variable to operate on.

Can static methods reference non-static variables?

Yes, a static method can access a non-static variable. This is done by creating an object to the class and accessing the variable through the object.

How do you reference a non-static variable from a static context?

In a simple way, we have to create an object of the class to refer to a non-static variable from a static context. A new copy of all the non-static variables is created when a new instance of variable is created. So, we can access these variables by using the reference of the new instance of the class.


1 Answers

main is a static method. It cannot refer to balance, which is an attribute (non-static variable). balance has meaning only when it is referred through an object reference (such as myAccount.balance or yourAccount.balance). But it doesn't have any meaning when it is referred through class (such as Account.balance (whose balance is that?))

I made some changes to your code so that it compiles.

public static void main(String[] args) {
    Account account = new Account(1122, 20000, 4.5);
    account.withdraw(2500);
    account.deposit(3000);

and:

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

public void deposit(double depositAmount) {
    balance += depositAmount;
}   
like image 127
wannik Avatar answered Sep 19 '22 06:09

wannik