0

I'm having trouble accessing this when writing an anonymous function.

public class Game extends JPanel {
    public void action() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                this.repaint();
            }
        }).start();
    }
    @Override
    public void paint(Graphics g) {
        // Paint stuff
    }
}

I can't declare a new instance of my class because I can't set the class to my new instance of the class without getting static/non-static errors.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
ChaiNunes
  • 68
  • 12

1 Answers1

2

When you write

 public void action() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                this.repaint();
            }
        }).start();
    }

Since you are in writing anonymous inner class Runnable , you refer to Runnable anonymous class when you write this.repaint().

To refer the Game class repaint, the syntax is Game.this.repaint()

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307