1

this is rather a theoretical question but maybe you know the specification that deep that will let you answer... Why this code yields false in terms of if the anonymous class is final? In practice the class can be considered final (there is no way to extend it without bytecode manipulation):

public class Modifiers
{
    public static void main(final String[] args) throws ClassNotFoundException
    {

        new Modifiers().go();
    }

    public void go() throws ClassNotFoundException
    {
        final Runnable r = new Runnable()
        {
            @Override
            public void run()
            {
                System.out.println("Inside runnable");
            }
        };
        r.run();
        System.out.println(Modifier.isFinal(getClass().getClassLoader().loadClass(Modifiers.class.getName() + "$1").getModifiers()));
    }
}

1 Answers1

4

Because the anonymous inner class that you are checking, Modifiers$1, is not final.

The variable r is final, but that does not mean that the class itself is final.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • This is not the thing I'm asking about. The filed has noting to the fact that the class Modifier$1 is not considered final. Imagine the following declaration without a field: new Runnable(){ public void run(){}}.run(); The behaviour will be exactly the same... – jestem_wojtek Aug 08 '14 at 12:02
  • As you found out the anonymous inner class is simply not `final`. Why does it matter to you that it is not `final`? – Jesper Aug 08 '14 at 12:39