Craig Otis Craig Otis - 1 year ago 62
Java Question

Does adding the 'final' keyword to methods in anonymous classes have any effect?

Note: I'm not asking the age-old question about why outer variables accessed in an anonymous class need to be declared final.

When creating an anonymous class in Java, you can add additional methods if you desire:

Runnable r = new Runnable() {
public void run() {

public void internal() {
.. code ..

However, Java also allows you to declare the additional methods as

public final void internal() { ... }

My question is: Aren't those methods already effectively final, and does adding the
keyword have any effect?

Answer Source

The Java specification about final methods says:

A private method and all methods declared immediately within a final class (§ behave as if they are final, since it is impossible to override them.

and from Anonymous Class Declarations:

An anonymous class is always implicitly final (§

Therefore, an anonymous class is already final, which makes all of its methods final. You can add the final modifier but it is redundant.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download