-
Static problem
Why can't you use a non static variable in a static method? Right now i have a boolean variable and Scanner named scan Screaming in my main(String[]args) that it needs to be static. I have declared the variables above, is there something to do with the constructor?
Code:
import java.util.Scanner;
public class Game {
private Player you;
private Player comp;
private Game game;
private boolean yturn;
String inpt;
Scanner scan;
public int win, total;
private int die1, die2;
Dice dice = new Dice();
public Game(){
die1 = dice.getDie1();
yturn=false;
}
public static void main(String[] args) {
Game game = new Game();
[COLOR="Red"]scan[/COLOR] = new Scanner(System.in);
System.out.println("Welcome to Pig v3.5\nPlease enter your name:");
String inpt = [COLOR="red"]scan[/COLOR].nextLine();
System.out.println(inpt+", you will be against this Computer. Please hit enter to begin your turn.");
inpt = [COLOR="red"]scan[/COLOR].nextLine();
[COLOR="red"]yturn[/COLOR] = true;
[COLOR="red"]play[/COLOR]();
}
Oh and now it looks like my Play(); Method is also needing to be called as static. All i did was move:
Code:
System.out.println("Welcome to Pig v3.5\nPlease enter your name:");
String inpt = scan.nextLine();
System.out.println(inpt+", you will be against this Computer. Please hit enter to begin your turn.");
inpt = scan.nextLine();
into my Main method. Grrrr Static is shared across the program. correct?
-
static things are associated witht he class as a whole.
non-static things are part of an instance of a class (ie an object).
So, though you've created an instance of Game, scan is not actually visible outside of that Game object.
Your main() method is not part of the same object that scan is.
Your best bet is to move that code (after the Game declaration) into a method in Game that you then call:
Code:
Game game = new Game();
game.run(); // or some suitable name.
-
It makes more sense for your Scanner to be static anyway. You only have one keyboard, so why do you need more than one Scanner? You can put the rest of your code into your play() method, and call it as Tolls suggested, or simply do:
Code:
public static void main(String[] args) {
[COLOR="Blue"] new Game().play();
[/COLOR] }
-Gary-
-
By the way, why does your Game class have a Game game instance variable?
-Gary-
-
I was actually confused on instantiating other classes that i had. But i am not sure if i even need one. I don't actually return anything in my Game Class. Also I did end up changing my Scanner to being static, but i am still having issues with my boolean variable. Would changing the variable to static even be acceptable?
-
No, you probably don't need to instantiate a class, but it's a good habit. Writing everything as static is not how a normal Java program is written, so you don't want to fall into a bad habit so early on.
SHow us your latest code if you need more help.
-
Code:
import java.util.Scanner;
public class Game {
private Player you;
private Player comp;
private Game game;
private boolean yturn;
String inpt;
static Scanner scan;
public int win, total;
private int die1, die2;
Dice dice = new Dice();
public Game(){
die1 = dice.getDie1();
yturn=false;
}
public static void main(String[] args) {
Game game = new Game();
scan = new Scanner(System.in);
System.out.println("Welcome to Pig v3.5\nPlease enter your name:");
String inpt = scan.nextLine();
System.out.println(inpt+", you will be against this Computer. Please hit enter to begin your turn.");
inpt = scan.nextLine();
[COLOR="Red"]yturn[/COLOR] = true;
game.play();
}
yturn still wants to be static
-
We did suggest moving all that code after the "game" instantiation into a method in Game and then call it via game. That would solve all these problems.
ETA: The main() method should really not do any processing. It is simply there to get your code started.