-4

I'm fairly new with Java and I'm having some trouble understanding what I'm doing wrong. Here is a brief description of the purpose of the program.

  1. Make a bank account.
  2. Deposit 1000 into it.
  3. Withdraw 400.
  4. Withdraw 500.
  5. Print out results and expected results.

Here is my code. Keeps on saying non-static variable bankAcc cannot be referenced from a static context.

 public class BankAccountTester
    {
        private double bankAcc; //Stores bankAcc balance

        public void money(double deposit)
        {
            deposit= (1000);

            int withdraw1 = -400;
            int withdraw2= -500;
            bankAcc= bankAcc + withdraw1 + withdraw2;
        }

        public double getbankAcc()//Returns value to bankAcc so it has new balance
        {
            return bankAcc;
        }

        //Prints out value and expected value
        public static void main(String[] args){
            System.out.Println("My bank account has " + bankAcc);
            System.out.Println("Expected is 100");
        }
    }
harpun
  • 4,022
  • 1
  • 36
  • 40
user2872518
  • 53
  • 1
  • 3
  • 8

6 Answers6

0

main() is a static method i.e. no class instance is associated with it. While bankAcc is an instance member of BankAccountTester class and hence cannot be accessed without creating its instance first. Test your program with an object instance available as:

public static void main(String[] args){
    BankAccountTester bat = new BankAccountTester();
    bat.deposit(0.0);
    System.out.Println("My bank account has " + bat.getbankAcc());
    System.out.Println("Expected is 100");
}

Also, see here.

Community
  • 1
  • 1
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

When you write a class, there are two "flavours" of class content. Static, which exists as global properties tied to "the class" and non-static, which lives on individual objects that you build using that class definition. As such, your code -to java- looks like this:

  • for objects: private double bankAcc, public void money, public double getbankAcc
  • for global class access: public static void main

static code exists irrespective of whether any objects have been built, so you can't tell a static method that it should access an object variable: it doesn't exist as far as it knows. Even if you do create an object from this class, it will locally have a variable called backAcc, but it's not statically accessible.

The general recipe you want to follow is this:

public class Test {
    private long whatever = 123456;
    public Test() {
      // real code goes here.
      System.out.println("my whatever variable holds " + whatever);
    }
    public static void main(Sting[] args) {
        // build an object based on the Test class.
        // and let it handle everything else.
        new Test();
    }
}

When you compile and run this code, the Test class will have a static (=globally callable) method main, which builds an actual Test object. Before you do, there are objects to work with, only the class definition exists. Once you build a Test object, it can then do everything you need to do, in a nice object-oriented way.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
0

First, static (re. a variable) means that there exists one global instance of that variable for the class. This is opposed to simply private double bankAcc;, which is saying that each instance of the class has its own bankAcc.

More particularly to your problem, since main is static, it is not a method on an instance of BankAccountTester. This means that, when you are trying to print out bankAcc, you are trying to access a non-static variable from a static context, which is not allowed.

Without seeing where exactly you use money and getbankAcc, you can fix this by changing:

private double bankAcc;

to:

private static double bankAcc;

CmdrMoozy
  • 3,870
  • 3
  • 19
  • 31
0

The variable bankAcc is an instance variable, meaning that it only exists when you create an instance of BankAccountTester (using new BankAccountTester()). Since you are only calling it from the static main() method without creating an instance, there is no bankAcc variable. Change the declaration to private static double bankAcc; to make your program work.

0

Since main is a static method, it can only refer to static variables and methods. A static variable looks like:

private static double bankAcc;

As you have it written, bankAcc is an instance variable, meaning it's tied to a specific instance of BankAccountTester.

Since you don't have any instances (i.e., you have not created a new BankAccountTester()), you can only refer to the static parts of BankAccountTester.

Alex Wittig
  • 2,800
  • 1
  • 33
  • 42
0

This is basic Java. That's why someone has voted your question down. But here's the answer.

In Java, the most common way to execute code is to reference a class that contains a main method when starting a JVM via the java command. For instance:

java com.me.MyClass

This will start a jvm and look for a main method on MyClass to execute. Note, main method is static.

On the other hand, Java classes most commonly define "classes". These are the definition of object structure. An object is a runtime instance of a class, complete with it's own memory. Your field bancAcc is an instance field, as opposed to a static field. That means each object instance of your class BankAccountTester will have it's own dedicated memory for hold a value of a bankAcc. Note, this doesn't exist unless you create an ojbect.

So, in your code, you haven't created an instance object. You could do so with the new constructor, and then reference the bankAcc on that instance object; note, there is no bankAcc unless there's an instance object. So . . .

BankAccountTester accTester = new BankAccountTester();
accTester.bankAcc = 100.00;
System.out.Println("My bank account has " + accTester.getBankAcc() );

Note, you have been confused because you have wrongly assumed that the main method's existence in your class has something to do with the class defined therein. The placement of the main here is arbitrary and unrelated to your actual class definition. To clarify it in your head, you should create two classes, one that defines the bank account, and another that is your "bootstrapper" class.

The bootstrapper will contain ONLY a main method, and it will create instances of the objects, defined by classes found in separate class files, and execute methods on them.

chad
  • 7,369
  • 6
  • 37
  • 56