blueButton.addActionListener(new blueButtonListner());
What happens when this code is entered?
What I think is Java compiler creates an object called blueButtonListner() and it becomes an input to (parameters for) addActionListener
If that is correct as I guessed then this code should also work:
redButton.addActionListener(rr);
redButtonListener rr =new redButtonListener();
But it shows an error. Can someone explain this to me?
It’s a matter if precedence, you can’t have something until it’s created
Won’t work, because
rrhasn’t been defined yet, the compiler has not idea of what it is.In contrast
The compiler creates a temporary Object and passes it to the
addActionListenermethod.You can correct your code with this