Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Non-static variable cannot be referenced in a static context error

    Hello. My apologies in advance for the silly question. I wrote a simple program to make use of constructor methods. I'm getting a "non-static variable cannot be referenced from a static context" error on compiling.

    I understand from reading other similar queries on this subject that I should instantiate either a FourDice or PairOfDice object in my main method. But I haven't been able to make either of those work when I tried it.

    Can anyone show me how to make this work?

    Here is the code. The error message references the first two lines in the Main method.

    Java Code:
    // This program outputs the number of times 2 pairs of dice
    // must be rolled to give the same face values.
    
    public class FourDice {
    	public class PairOfDice {
         	public int die1, die2;
    
    		public PairOfDice() {
    			roll();
    		}
    		public void roll() {
    			die1 = (int)(Math.random()*6) + 1;
    			die2 = (int)(Math.random()*6) + 1; 
    		}
    	}
    
    	public static void main(String[] args) {
    		PairOfDice pair1 = new PairOfDice();
    		PairOfDice pair2 = new PairOfDice();
    	
    		int total1, total2;
    		int countRolls = 0;
    
    		do {
    			pair1.roll();
    			total1 = pair1.die1 + pair1.die2;
    
    			pair2.roll();
    			total2 = pair2.die1 + pair2.die2;
    
    			countRolls++;
    			System.out.println("The first pair totaled " + total1 + ", and the second pair 				totaled " + total2);
    		} while (total1 != total2);
    
    		System.out.println("It took " + countRolls + " for the dice to show the same totals.");
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Non-static variable cannot be referenced in a static context error

    PairOfDice is an inner, non-static class. That means an instance of it cannot exist unless you have an instance of the enclosing class. So to allocate an instance of PairOfDice within a static context, do this:

    Java Code:
    FourDice fd = new FourDice();
    PairOfDice pair1 = fd.new PairOfDice();
    PairOfDice pair2 = fd.new PairOfDice();
    You need the FourDice reference because main is a static entry point. If you had a non-static method within FourDice, you could allocate an instance by just using the following:

    Java Code:
    PairOfDice pd = new PairOfDice();
    Regards,
    Jim
    Last edited by jim829; 04-09-2013 at 11:58 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Non-static variable cannot be referenced in a static context error

    Thank you Jim - that did the trick.

    Your explanation was much more clear to me than those I have read in various textbooks. I appreciate the help.

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 01:00 PM
  2. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  3. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  4. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

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