Home > Java > javaTutorial > Why am I getting the Java error \'Cannot Make a Static Reference to the Non-Static Field\'?

Why am I getting the Java error \'Cannot Make a Static Reference to the Non-Static Field\'?

Susan Sarandon
Release: 2024-11-01 18:10:02
Original
405 people have browsed it

Why am I getting the Java error

Java Error: "Cannot Make a Static Reference to the Non-Static Field"

This error arises when attempting to access a non-static field within a static method. In Java, static methods belong to the class and can only access static variables, while non-static methods (instance methods) belong to an instance of the class and can access both static and non-static variables.

In your code, the main method is static, and it tries to call the withdraw and deposit methods, which are non-static. The error suggests that you're trying to refer to the balance field from within a static context.

Resolution:

To resolve this issue, you need to make the withdraw and deposit methods static as well. However, it's not advisable to modify the original methods as they are intended to operate on specific account instances. Instead, create overloaded versions of these methods within the main method.

Here's the updated code:

<code class="java">public class Account {

    // Static variables
    public static int id = 0;
    public static double annualInterestRate = 0;
    public static java.util.Date dateCreated;

    // Non-static variables
    private double balance = 0;

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

        // Overloaded methods (static)
        double newBalance = withdraw(account.balance, 2500);
        newBalance = deposit(newBalance, 3000);

        System.out.println("Balance is " + account.getBalance());
        System.out.println("Monthly interest is " + (account.annualInterestRate / 12));
        System.out.println("The account was created " + account.getDateCreated());
    }

    // Overloaded methods
    public static double withdraw(double balance, double withdrawAmount) {
        balance -= withdrawAmount;
        return balance;
    }

    public static double deposit(double balance, double depositAmount) {
        balance += depositAmount;
        return balance;
    }
}</code>
Copy after login

By creating static overloaded methods, you can now access the balance field within the main method. These overloaded methods take the current balance as a parameter, perform the operations, and return the updated balance.

The above is the detailed content of Why am I getting the Java error 'Cannot Make a Static Reference to the Non-Static Field'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template