static help

I know that there are a lot of posts with this same error but as i was running through thier code i could fix my own. This is a simple program for my java class but we are on break right now. Im getting the error when i try to get a boolean value from another class. Thank in advance for all the help.

So here is the error:

non-static variable rolledYet cannot be referenced from a static context

Here is the code:

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

publicclass Diceextends Appletimplements ActionListener{

private Button roll =new Button("Roll");

private Die die1 =new Die(100);

private Die die2 =new Die(120);

publicboolean rolledYet =false;

publicvoid init(){

roll.addActionListener(this);

add(roll);

}

publicvoid actionPerformed(ActionEvent event){

if(event.getSource() == roll){

die1.roll();

die2.roll();

}

rolledYet =true;

repaint();

}

publicvoid paint(Graphics g){

die1.display(g);

die2.display(g);

}

}

class Die{

privateint dieValue, yCoord;

public Die(int initialYCoord){

yCoord = initialYCoord;

}

publicvoid roll(){

dieValue = (int) (Math.random() * 6 + 1);

}

publicvoid display(Graphics g){

if (Dice.rolledYet)

g.drawString("Die Value: " + dieValue, 100, yCoord);

}

}

[3192 byte] By [ltu0105a] at [2007-9-28 12:45:25]
# 1

you can replace <public boolean rolledYet = false;> to <public static boolean rolledYet = false;> Be Careful of this, it only works if u have one Object of your class Dice

Or the better way, you can replace

public void display(Graphics g) {

if (Dice.rolledYet)

g.drawString("Die Value: " + dieValue, 100, yCoord);

}

to

public void display(Graphics g, Dice d) {

if (d.rolledYet)

g.drawString("Die Value: " + dieValue, 100, yCoord);

}

This gives a reference of your Dice object. Now u must call the method like this:

die1.display(g,this);

tricuta at 2007-7-12 3:59:58 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Thank you so much... worked great. I appreciate the help.~Nathan
ltu0105a at 2007-7-12 3:59:58 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...