2

There is somewhat a similar question but here the scenario is different.

You are required to make a method final if you want to use @SafeVarags as in:

@SafeVarags
public final void myMethod(Whatever... args)
{
    // code here
}

OK, fine. But then you are required to make the method final even in this situation:

// Class itself is final...
public final class MyClass
{
    // ...

    @SafeVarags
    public void myMethod(Whatever... args) // COMPILE ERROR!
    {
        // code here
    }
}

Why isn't the fact that the class itself is final enough for the compiler? Is this because of some obscure possible "shoot-yourself-in-the-foot" reflection?

Community
  • 1
  • 1
fge
  • 119,121
  • 33
  • 254
  • 329
  • In general, the compiler doesn't concern itself with "logical" problems or implications. Rather it concerns itself with types. Here, the annotation dictates that the method be final. Simple. It doesn't concern itself with the practicality that all methods of a final class are effectively final. – Bohemian May 26 '15 at 07:58

0 Answers0