This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

What is binary compatibility in Java?

I’m reading Effective Java by Joshua Bloch.

In Section 17, I came across an explanation that it is not recommended to use interfaces to store constants. I’ll explain below.

“Worse, it represents a promise that if the class is modified in a future release so that it no longer needs constants, it will still have to implement the interface to ensure binary compatibility.”

What does binary compatibility mean here?

Can someone guide me with Java examples to prove what code is binary compatible?

Answer 1:

In short, binary compatibility means that when you change a class, you don’t have to recompile the classes that use it. For example, you remove or rename public or protected methods from this class

public class Logger implements Constants {
   public Logger getLogger(String name) {
         returnLogManager.getLogger(name); }}Copy the code

After publishing from your log-1.jar library, you publish a new version, log-2.jar. When log-1.jar users download the new version, the application will not run when they try to use the deleted getLogger (String Name) method.

Since you remove the constant interface, this also breaks binary compatibility for the same reason.

However, you can remove/rename private or Package private members of this class without breaking binary compatibility, because external applications cannot (or should not) use it.

It’s kind of like the Idea of jAVA programming, and the private part is very similar to that. Think as a JAR developer.