0

Here I am trying to have my 'cash' variable from the Player method be included in an equation in my wagerBet() method. Currently Eclipse is telling me that variable 'cash' cannot 'make a static reference to the non-static field cash'. I tried looking for explanations as to what this problem means but I'm just getting explanations that use even more programming terminology I don't understand being that I'm a rookie at this stuff.

class Player {
    private ArrayList<Card>hand;
    private double cash, bet;

    public Player(double theCash)
    {
        cash = theCash; //'cash' variable here
        hand = new ArrayList<Card>();
    }

    public static double wagerBet()
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Wager a bet: "); 
        double bet = in.nextDouble();
        cash = cash - bet; // needs to be transferred here
        System.out.println("You wagered " + bet + ". " + "Now you have " + cash + " cash left.");
        return bet;
    }

    public void rewardBet(double bet)
    {
        cash = cash + (bet * 2); //cash and bet variable needs to be transferred here as well
        System.out.println("You now have " + cash + "cash.");
    }

Any suggestions?

morgano
  • 17,210
  • 10
  • 45
  • 56
SJSharks
  • 39
  • 2
  • 9
  • possible duplicate of [cannot make a static reference to the non-static field](http://stackoverflow.com/questions/8101585/cannot-make-a-static-reference-to-the-non-static-field) – user207421 Mar 17 '14 at 01:14

4 Answers4

2

The cash variable doesn't belong to any method. It is an instance member of the class. You can't access it from a static method. Make the method non-static, or pass 'cash' to it as a parameter if you only need to read its value there.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • That fixes the problem for transferring the cash variables over to the 2 methods but my rewardBet() method still doesn't transfer over the bet variable from the wagerBet() method. Would I have to change anything about wagerBet() in order to make it accept the output of another method? – SJSharks Mar 17 '14 at 01:15
  • What do you mean by 'transfer over'? It returns it as a value. You just need to store the result when you call it. – user207421 Mar 17 '14 at 01:55
1

You might want to go through some Java basics:

http://docs.oracle.com/javase/tutorial/java/javaOO/index.html

You are attempting to use a property of Player in a static method. A static method is not part of an instance so it doesn't know what "cash" (as it's unique to every Player instance rather than a single variable).

Remove the "static" from wagerBet so that it becomes a method of Player. That way it's unique to every Player and so it'll know to use the "cash" of the same Player it's a part of.

Jahed
  • 484
  • 2
  • 8
0

remove static at

public static double wagerBet()
Gee858eeG
  • 185
  • 8
0

There is only one real problem. To understand what static is.

Basic approach, until you are good enough, is to NOT use static at all. Remove it in all cases instead of "main" (you have to keep it there).

libik
  • 22,239
  • 9
  • 44
  • 87