2
public class GameMenu extends JPanel{

    private final Core core;
    private final JButton loadGame;
    private final JButton saveGame;
    private final JButton exit;
    private final JButton newGame;

    public GameMenu(Core core){
        setPreferredSize(new Dimension(600, 600));
        setBackground(Color.BLACK);
        this.core = core;

        newGame = new JButton(newGameAction);
        loadGame = new JButton(loadGameAction);
        saveGame = new JButton(saveGameAction);
        exit = new JButton(exitGameAction);


        add(newGame);
        add(loadGame);
        add(saveGame);
        add(exit);
    }


  private Action newGameAction = new AbstractAction("New Game") {
        @Override
        public void actionPerformed(ActionEvent e) {          
            this.core.
        }
    };

HI, i want to call a method of core instead of call a method of this class which would call the proper method of core The problem is that i dont know how to reach the core field cos its cannot find thx

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
fagyi
  • 93
  • 2
  • 8

4 Answers4

4

In this context:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        this.core. // compiling error here: 'core' is not a member of the anonymous inner class
    }
};

The keyword this refers to the anonymous inner class generated when you create a new Action instance (the current object). This fact is explained here: Using the this Keyword:

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

Since the anonymous inner class has no member called core then you get a compiling error. In order to solve this problem take a look to this topic: Keyword for the outer class from an anonymous inner class?.

For practical purposes you should replace the line above for this one to refer to your GameMenu class:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        GameMenu.this.core. 
    }
};

Or you can remove this keyword and compiler will resolve the same for you:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        core. // should be recognised as outer class member
    }
};

Given the anonymous inner class has no core member, the compiler will look up for the outer class (or eventually the hierarchy tree, with the appropriate visibility) and chek if it has such memeber, which is indeed the case.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
3

You are in an inner class. Use:

GameMenu.this.core...

Otherwise, this refers to the anonymous inner class you are in.

David ten Hove
  • 2,748
  • 18
  • 33
2

With the declaration

private Action newGameAction = new AbstractAction("New Game") { ... }

you create an instance of an anonymous subclass of AbstractAction. The keyword this then refers to this instance and not to the outer instance of type GameMenu. So there is no core field available.

The syntax for accessing the core field of the outer GameMenu instance is:

GameMenu.this.core

But - as you are still inside the GameMenu class - you can access private members at this point, too. So you simply can write:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        doSomethingWith(core);
    }
};
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0

Remove this from your statement as you are defining an anonymous class where this would mean instance of your anonymous class itself and in your anonymous class you don't have field called core. so use it like:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        //core.
    }
};
SMA
  • 36,381
  • 8
  • 49
  • 73