Does removing the “final” keyword affect binary compatibility?
-
13-06-2021 - |
Question
If I remove the final
keyword from a method or other "thing", will users of my class have to recompile?
Solution
Technically, they won't have to recompile.
I cannot think of any repercussions that can result from removing the final keyword from a method / attribute which may lead to loss in compatibility so it should not give you any problems.
Tested with sample code and there were no runtime errors:
public class Test2{
public static final String test = "HELLO!";
}
public class Test {
public static void main (String [] args) {
System.out.println(Test2.test);
}
}
- Compiled Test.java
- Ran Test.java -> Output = "HELLO!"
Modified Test2.java:
public class Test2{ public static String test = "HELLO!"; }
Compiled Test2.java
- Ran Test.java -> Output = "HELLO!"
OTHER TIPS
No, removing the final keyword will not break compatibility assuming you're fine with this wart:
If module B calls any method from a class in module A and module C overrode the previously final method, and an object from module C is passed to module B, the call will go to module A's implementation.
No if your clients call your code directly. But is might break a program that discovers your class using reflection, checks whether your method is final and does different things if method is final or not.
Straight from the horse's mouth:
Changing a method that is declared final to no longer be declared final does not break compatibility with pre-existing binaries.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html#jls-13.4.17