2

I have a homework assignment where I am supposed to simulate the roll of a dice using math.random() and changing it to an int. I have a single file with 2 classes and am attempting to make an object. My code compiles with the run time error "error: non-static variable this cannot be referenced from a static context." Any Idea what is happening.

I have changed the value of "value" to an integer and successfully ran the code. No other changes have come to mind yet.

public class DieTester_5AlastiCorrigan {

    public static void main(String[] args){

        // New object myDie. 
        Die myDie = new Die();
        System.out.println(myDie.roll());
        System.out.println(myDie.getValue());
    }

    // Creates a new Die Class 
    class Die{
        private String value;

        public Die( int dieRoll ) {
            value = "" + dieRoll;

        }

        // Roll Method chooses random number between 1 - 7 and makes it    an int. 
        public int roll() {
            int max = 6;
            int min = 1;
            int range = max + 1;

            int dieRoll = (int)Math.random()*range;
            //dieRoll = (int)dieRoll;
            return dieRoll;
        }
        // getValue Method returns final value of "value". 
        public String getValue() {
            return value;
        }
    }


}

Expect the console to print out a number 1 <= x < 7 as an integer.

Error message: error: non-static variable this cannot be referenced from a static context Die myDie = new Die(); ^

AlastiAF
  • 23
  • 3

2 Answers2

2

Notice how your Die class is inside your DieTester_5AlastiCorrigan class. That makes it an non-static inner class. You would need an instance of DieTester_5AlastiCorrigan to create an instance of Die. So to fix this, simply move Die to the top level, like this:

class DieTester_5AlastiCorrigan {
    ...
}

class Die {
    ...
}

Or add a static modifier:

class DieTester_5AlastiCorrigan {
    ...

    static class Die {
        ...
    }
}

However, there are still a few mistakes in your code. Die has a constructor that takes an int, but when you are creating a Die, Die myDie = new Die();, you are not passing an int to the constructor. I suggest that you add a parameterless constructor:

public Die() {
    this(1);
}

Also, value should not be of type String. It should be an int, and judging from your usage, roll should change the value of value instead of returning the die roll.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

class Die is an instance variable of class DieTester_5AlastiCorrigan , which means you can only create a Die instance with a DieTester_5AlastiCorrigan instance. This code should run:

DieTester_5AlastiCorrigan outerObject = new DieTester_5AlastiCorrigan();
DieTester_5AlastiCorrigan.Die myDie = outerObject.new Die();
  • 1
    but why create an outer Object instance that has no functionality at all? that is at most a workaround, not a solution – user85421 Sep 10 '19 at 05:03
  • 1
    because i thought that the way you wanted, if you dont want to create outer object then just declare Die as static, like this : static class Die {.. – Nguyen Tan Bao Sep 10 '19 at 06:04
  • 1) I am not the author of the question; 2) it is not a matter of wanting or not; programming just to avoid compilation errors will not work very well on bigger (real) projects - *if your cars door is not closing correctly, the solution is not just removing the door* 3) die `Die` constructor expects an argument, but that would be the next error (Why not just move the `Die` class outside the tester class, if not moving to an own file? It does not need, by given coe, to be a nested class at all) – user85421 Sep 10 '19 at 07:22
  • i guess this is just a small test, and he just forgot to declare the inner class as static – Nguyen Tan Bao Sep 10 '19 at 07:24