-1

Hi I'm pretty sure this is a beginner JAVA question. I ran my compiler and it showed that there is an error

Cannot make a static reference to the non-static method roll() from the type Die 

on the last line of my main method.

What I am trying to do is roll two dices and add them up. My question is what is wrong with this line
and how do I fix this? Thanks in advance

       /** main method

       Die myDie1 = new Die();
       Die myDie2 = new Die();



      for(int roll=1;roll<=total;roll++)
          {
              counts[(Die.roll()+Die.roll())]++; //<--error here
          } 
       **/

Dice method

   public class Die
     {
       private final int MAX = 6;  // maximum face value

       private int faceValue;  // current value showing on the die

       //-----------------------------------------------------------------
       //  Constructor: Sets the initial face value.
       //-----------------------------------------------------------------
        public Die()
        {
         faceValue = 1;
         }

       //-----------------------------------------------------------------
       //  Rolls the die and returns the result.
       //-----------------------------------------------------------------
       public int roll()
      {
        faceValue = (int)(Math.random() * MAX) + 1;

        return faceValue;
      }

       //-----------------------------------------------------------------
       //  Face value mutator.
       //-----------------------------------------------------------------
        public void setFaceValue (int value)
       {
          faceValue = value;
       }

       //-----------------------------------------------------------------
       //  Face value accessor.
       //-----------------------------------------------------------------
        public int getFaceValue()
       {
           return faceValue;
         }

     //-----------------------------------------------------------------
     //  Returns a string representation of this die.
     //-----------------------------------------------------------------
      public String toString()
    {
       String result = Integer.toString(faceValue);

       return result;
     }
   }
Anptk
  • 1,125
  • 2
  • 17
  • 28
Ai Zhu Xue
  • 23
  • 4
  • possible duplicate of [Cannot Make Static Reference to Non-Static Method](http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to-non-static-method) – Dennis Meng Oct 29 '14 at 06:02

4 Answers4

1

Change the line

counts[(Die.roll()+Die.roll())]++;

to

counts[myDie1.roll()+ myDie2.roll()]++;

Note there is a missing left parenthesis too

Chiseled
  • 2,280
  • 8
  • 33
  • 59
0

You have to create an object of type Die or make your method static. Maybe you should have a look at static classes and variables as well as java objects and their differences.

One way is:

Change:

public int roll()
{
        faceValue = (int)(Math.random() * MAX) + 1;

        return faceValue;
}

To:

public static int roll()
{
        faceValue = (int)(Math.random() * MAX) + 1;

        return faceValue;
}

The better and Java OO way is to change from

for(int roll=1;roll<=total;roll++)
{
    counts[(Die.roll()+Die.roll())]++;
} 

to:

for(int roll=1;roll<=total;roll++)
{
    counts[(myDie1.roll()+myDie2.roll())]++; 
} 
dazito
  • 7,740
  • 15
  • 75
  • 117
0

You probably just want to replace:

counts[(Die.roll()+Die.roll())]++;

with:

counts[(myDie1.roll()+myDie2.roll())]++;

Call the instantiated methods on the objects, not the uninstantiated methods on the class

CharlieS
  • 1,432
  • 1
  • 9
  • 10
0

That is because u giving Static method ref into the non static object.

according to the java u can not call method threw direct reference. so u need to make that method static.

so change your method according to this .

 public static int roll()
      {
        faceValue = (int)(Math.random() * MAX) + 1;

        return faceValue;
      }

and after you update this this will work

for(int roll=1;roll<=total;roll++)
          {
              counts[(Die.roll()+Die.roll())]++; //<--error here will be solved
          } 

hope this helps.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68