-2

EDIT: MyDrawPanel is this:

class MyDrawPanel extends JPanel {
    public void paintComponent(Graphics g) {
        g.setColor(Color.RED);
        g.fillOval(d, e, 40, 40);
    }
}

I'm having a frustrating problem with this small lump of code. I'm trying to create a new MyDrawPanel. Code:

class Simulation{
MyDrawPanel drawPanel = new MyDrawPanel();
public static void startSimulation() {
    frame.getContentPane().add(drawPanel); //drawPanel must be static
}
}

Here's the issue: Eclipse tells me that drawPanel must be static. But, whenever I change it to static, Eclipse gives an error basically telling me to get rid of the static.

I've done research, and this is what I found: https://stackoverflow.com/a/9560633/4778083 This person tells me that I should make the class static, but whenever I do, Eclipse tells me, "Illegal modifier for the class Simulation; only public, abstract & final are permitted."

So basically, I'm stuck. What is wrong with my code?

Community
  • 1
  • 1
Nick
  • 61
  • 8
  • Is it a class you wrote? – nom Jun 11 '15 at 16:57
  • why did you modify `startSimulation()` as `static`?when you want to call it?and where did you create frame? – Madhawa Priyashantha Jun 11 '15 at 16:58
  • How exactly do you make `MyDrawPanel` `static`? Do you make it static by saying `MyDrawPanel static drawPanel = new MyDrawPanel();` or `MyDrawPanel drawPanel = new MyDrawPanel() static` or `MyDrawPanel drawPanel = new static MyDrawPanel();`? You see, the possibilities are endless for it to not work. The right way of doing it is `static MyDrawPanel drawPanel = new MyDrawPanel();`. – Chetan Kinger Jun 11 '15 at 17:00
  • I modified 'startSimulation()' is static because it is run in the 'public static void main' section and the compiler won't let me run it without a static modifier. – Nick Jun 11 '15 at 17:00
  • Where's the declaration of `frame`? – RealSkeptic Jun 11 '15 at 17:01
  • Make it static like this: `static MyDrawPanel drawPanel = new MyDrawPanel()` – Nick Jun 11 '15 at 17:01
  • The declaration of frame is in another class. I know I have done that right. – Nick Jun 11 '15 at 17:02
  • In another class? Then how can you use it directly without stating an instance or a class name or passing it as a parameter? Is your `Simulation` an inner class? – RealSkeptic Jun 11 '15 at 17:03
  • Step one is to understand the difference between static and non-static. – Kevin Workman Jun 11 '15 at 17:10

1 Answers1

0

First of all, the answer you talk about is for a nested class, yours is not a nested class. Secondly, if you plan to call startSimulation() in main(), you will have to make the instance of Simulation static. You cannot make a whole class declaration static, but only the instance of a class static. You should really read more about what static is.

What I mean is:

public class SomeClass{
static Simulation s = new Simulation();
public static void main(String[] args){
s.startSimulation();
}
}

Edit: The instance of MyDrawPanel in Simulation will have to be static as well, because you cannot refer to a non-static variable from a static context.


"a static variable is a variable that has been allocated statically—whose lifetime or extent extends across the entire run of the program"Wikipedia

nom
  • 256
  • 3
  • 16