Non-final methods in a final class

后端 未结 3 2056
有刺的猬
有刺的猬 2020-12-13 04:29

My question is pretty simple:
Does the compiler treat all the methods in a final class as being final themselves? Does adding the final keyword to methods

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 04:45

    May be the compiler treats them as final.

    The following prints "false":

    final class FinalClass {
        public void testMethod() {}
    }
    
    Method method = FinalClass.class.getDeclaredMethod("testMethod");
    int m = method.getModifiers();
    System.out.println(Modifier.isFinal(m));
    

提交回复
热议问题