Cannot statically refer to non-static fields

I apologize in advance if this code is not formatted correctly, trying to insert instead of re-binding each line. If this is wrong, can someone tell me an easy way to insert multiple lines of code at once?

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

I tried to make static methods without a result and make the main method non-static by removing the "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; } } 
+6
source share
6 answers

the lines

 account.withdraw(balance, 2500); account.deposit(balance, 3000); 

you may want to take and enter non-static data and let them change the balance

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

and remove the balance parameter from the call

+6
source

main is a static method. It cannot refer to balance , which is an attribute (non-static variable). balance only makes sense when passed through a reference to an object (for example, myAccount.balance or yourAccount.balance ). But that doesn't make any difference when it is passed through a class (e.g. 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; } 
+10
source

Static calls to withdraw and deposit - your problem. account.withdraw (balance, 2500); This line cannot work because "balance" is an account instance variable. In any case, the code does not make much sense, will it not be delayed or deposited inside the Account object itself? so the review should be more like

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

Of course, depending on your problem, you can do an extra check here to prevent a negative balance, etc.

+1
source

You are trying to access a non-stationary field directly from a static method that is not legal in java. balance is an unsteady field, so either refer to it using the link to the object, or stat it.

+1
source

Just write:

 private static double balance = 0; 

and you can also write lines like this:

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

you can keep your withdrawal and deposit methods static if you want, but you have to write it like in the code below. sb = initial balance and eB = final balance.

 Account account = new Account(1122, 20000, 4.5); double sB = Account.withdraw(account.getBalance(), 2500); double eB = Account.deposit(sB, 3000); System.out.println("Balance is " + eB); System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12)); account.setDateCreated(new Date()); System.out.println("The account was created " + account.getDateCreated()); 
-1
source

Source: https://habr.com/ru/post/901329/


All Articles