0

I can't figure out how to use variables when methods are static (which "main" has to be?). Here is an example:

import java.util.Scanner;
public class Yatzy {
    Scanner input = new Scanner(System.in);
    protected static int reply;   

    public static void main(String[] args){
        startGame();
    }
    public static void startGame(){
        System.out.println("How many players? 1 - 3.");
        reply = input.nextInt(); // Option 1: class variable
        int userinput = input.nextInt(); // Option 2: instance variable
    }
}

No matter which way I try, I keep getting this error:

Error: non-static variable input cannot be referenced from a static context

How can I use user inputs (or more generally, variables) within static methods?

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
  • You should first learn the exact meaning and purpose of the `static` keyword. After you know that, the problem will be crystal clear. If something is `static` it does not belong to object instances anymore. If something is **not** `static`, it belongs to object instances. Like a class `Person` would have a member `age`. This member should be not static since every person has its own different age. If you would make it `static`, every person would share the exact same age since the variable then is not bound to specific instances anymore and shared among all instances. – Zabuzard Mar 17 '18 at 15:00
  • In your specific example, the variable `input` behaves like said `age`. It belongs to instances of `Yatzy`. So you would need to create an instance and call it from there: `Yatzy game = new Yatzy();` and then `game.input.nextInt();`. In your specific scenario I think it would be better if everything is **non-static** since it should be possible to create multiple `Yatzy` games and they should not interfere with each other. – Zabuzard Mar 17 '18 at 15:03
  • Please accept the answer which helped you most in solving your problem. It helps future readers. If the answers weren't helpful leave comments below them. So the poster can update them accordingly. – Roshana Pitigala Sep 23 '18 at 18:56

1 Answers1

1

The error message is clear, and this is a fundamental as well as a very basic concept in java. You are trying to refer to a variable within the static area, so you should create that variable in the static area.

static Scanner input = new Scanner(System.in);

will do the trick.


Using Static variables and methods

import java.util.Scanner;
public class Yatzy {
    static Scanner input = new Scanner(System.in);
    static int reply;   

    public static void main(String[] args){
        startGame();
    }
    public static void startGame(){
        System.out.println("How many players? 1 - 3.");
        reply = input.nextInt();
        int userinput = input.nextInt();
    }
}

In the above example, all methods and variables are in the static area.

Using Instance variables and methods

import java.util.Scanner;
public class Yatzy {
    Scanner input = new Scanner(System.in);
    int reply;   

    public static void main(String[] args){
        new Yatzy().startGame();
    }
    public void startGame(){
        System.out.println("How many players? 1 - 3.");
        reply = input.nextInt();
        int userinput = input.nextInt();
    }
}

Create a new object of your class in the main method and call startGame().

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
  • 1
    While this works, I think in this specific scenario it would be better to make everything non-static. Since it *should* be possible to create multiple instances of a `Yatzy` game. – Zabuzard Mar 17 '18 at 15:03
  • Well then, you'll have to create a new object of your class `Yatzy`, and then refer to your variables. However the `main` method must be `static` – Roshana Pitigala Mar 17 '18 at 15:05
  • 1
    Yeah, I mean you may add that, together with an example, to your answer to improve the quality, if you like. – Zabuzard Mar 17 '18 at 15:06
  • Thank you, this solved the problem. I was stuck with the (wrong) idea that the problem was with the "reply" variable, not with "input". – Jouni Kouvo Mar 17 '18 at 15:07
  • 1
    @Zabuza Sorry, I thought you were the person who asked the question... I added that too.. – Roshana Pitigala Mar 17 '18 at 15:08
  • 1
    @JouniKouvo Check the updated answer, and You are Welcome.. – Roshana Pitigala Mar 17 '18 at 15:09