-2

I have been working on a project, and I'm encountering a mistake which I can't seem to solve. I encounter an error "non-static variable initBalance cannot be referenced from a static context". I've researched what this is about, but I don't understand what it means.

Here is my code:

import java.util.Scanner;

public class ATM {
    Scanner kbd = new Scanner(System.in);
    int initBalance = 0;
    public static void main(String[] args) {
        System.out.print("\n---------------------ATM----------------\n1. Withdraw\n2. Deposit\n3. Transfer\n4. Check balance\n5. EXIT");
        System.out.print("\nChoose operation: ");
        int option = kbd.nextInt();
        while (option<5) {
            if(option==1)
                withdraw();
            else
                if(option==2)
                    deposit();
                else
                    if(option==3)
                        transfer();
                    else
                        if(option==4)
                            balanceCheck();

        }
    }
    
    public static void withdraw() {
        
        System.out.print("Enter amount to be withdrawn: ");
        int withdrawBalance = kbd.nextInt();
        if (withdrawBalance > initBalance) {
            System.out.print("Collect your money.");
            initBalance = initBalance - withdrawBalance;
            System.out.print("Chceck balance? 1. Yes 2. No : ");
            int balanceOption = kbd.nextInt();
            if (balanceOption==1) {
                System.out.print("Remaining balance: " + initBalance);
            }
        }
        
        else {
            System.out.print("Insufficient Balance");
        }
        
    }
    
    public static void deposit() {
        System.out.print("Enter amount you want to deposit: ");
        int depositBalance = kbd.nextInt();
        initBalance = initBalance + depositBalance;
        System.out.print("Chceck balance? 1. Yes 2. No : ");
            int balanceOption = kbd.nextInt();
            if (balanceOption==1) 
                System.out.print("Remaining balance: " + initBalance);
            else
                if(balanceOption==2)
                    System.out.print("\nThank you for using this ATM!");
                else
                    System.out.print("Number not in the option.");
                
    }
    
    public static void transfer() {
        System.out.print("Enter Account number: ");
        int accNum = kbd.nextInt();
        System.out.print("Enter amount to be transferred: ");
        int moneySent = kbd.nextInt();
        if(moneySent > initBalance) {
            System.out.print("Transfer Success!");
            initBalance = initBalance - moneySent;
            System.out.print("Chceck balance? 1. Yes 2. No : ");
            int balanceOption = kbd.nextInt();
                if (balanceOption==1) 
                System.out.print("Remaining balance: " + initBalance);
                else
                    if(balanceOption==2)
                        System.out.print("\nThank you for using this ATM!");
                    else
                        System.out.print("Number not in the option.");
        }
        else {
            System.out.print("Insufficient Balance");
        }
    
    }
    
    public static void balanceCheck() {
        System.out.print("Remaining balance: " + initBalance);
    
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 4
    Does this answer your question? [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Turamarth Nov 05 '21 at 13:16
  • I'm afraid not because in my problem i encounter the error when im calling a method. – Jason Wobblewitz Nov 05 '21 at 13:18
  • 2
    @JasonWobblewitz Have you read the linked duplicate *and* the error message? – QBrute Nov 05 '21 at 14:29

1 Answers1

0

Static methods (in your case: withdraw, deposit, transfer, balanceCheck) cannot use not static variable (in your case: initBalance). In order to use the non static variable you must create an object somewhere ( ATM anATM = new ATM()) and to use the static methods you do not create the object you just do for example: ATM.withdraw())

Tal Glik
  • 480
  • 2
  • 11