If this really works, then yellowButton is probably a class field that you have not noticed.
Check out the class again. You probably have something more:
class ButtonPanel extends JPanel implements ActionListener { private JButton yellowButton; public ButtonPanel() { yellowButton = new JButton("Yellow"); } }
If the variable foo cannot be found in the method scope, it automatically returns to this.foo . In contrast, some languages, such as PHP, do not have this flexibility. (For PHP, you always need to do $this->foo instead of $foo to access class fields.)
source share