Results 1 to 8 of 8

Thread: Static problem

  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Question 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?

    Java 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:

    Java 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?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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:
    Java Code:
    Game game = new Game();
    game.run();  // or some suitable name.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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:
    Java Code:
            public static void main(String[] args) {
    [COLOR="Blue"]                new Game().play();
    [/COLOR]        }
    -Gary-

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    By the way, why does your Game class have a Game game instance variable?

    -Gary-

  5. #5
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    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?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  7. #7
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Java 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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

Similar Threads

  1. String toCharArray problem - static context
    By Grendel0 in forum New To Java
    Replies: 10
    Last Post: 03-19-2010, 02:28 PM
  2. Problem with static attributes
    By AndreRodrigues in forum New To Java
    Replies: 5
    Last Post: 01-03-2010, 08:31 AM
  3. Install4j - bundling a static JRE 1.6 problem
    By banderas in forum Java Software
    Replies: 0
    Last Post: 02-13-2009, 11:23 AM
  4. Usual problem: cannot make a static....
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 12-15-2008, 04:19 PM
  5. Static Vector Problem
    By calicocal in forum New To Java
    Replies: 12
    Last Post: 11-10-2008, 12:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •