Final class methods are not implicitly final
All methods declared in a final class are implicitly final ?
That's a good word, one which the JLS uses fairly often when it's actually called for. However I don't think you'll be able to find any statement in the JLS which implies that methods of a final class are implicitly final. That's probably because the JLS authors felt it was really irrelevant whether the methods of a final class were technically final or not - as long as you can't extend the class itself, there's no possible way to override one of its methods. Regardless of whether the methods themselves are "final" or not. The important point is that a final class cannot ever be extended, which means that a method of a final class cannot ever be overridden. As long as you understand that, the above statement is really irrelevant.
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public final class Test1 {
public static void main(String[] args) throws Exception {
Class [] c = new Class[3];
c[0] = args.getClass();
c[1] = int.class;
c[2] = double.class;
Method mainMethod = Test1.class.getMethod("call", c);
System.out.println("Modifiers: " + mainMethod.getModifiers());
boolean isFinal = Modifier.isFinal(mainMethod.getModifiers());
System.out.println("main method is final: " + isFinal);
Class[] cc = mainMethod.getParameterTypes();
System.out.println("parameters of the method: " + mainMethod.getName() );
for( int i=0;i
System.out.println("parameter " + i + ": " + cc[i]);
}
}
}
}
Output:
main method is final: false
parameters of the method: call
parameter 0: class [Ljava.lang.String;
parameter 1: int
parameter 2: double
0 Comments:
Post a Comment
<< Home