exceptions

java.lang.IncompatibleClassChangeError – How to resolve Incompatible Class Change Error

In this example we will discuss about IncompatibleClassChangeError. This is thrown when an incompatible class change has occurred to some class definition. The definition of some class, on which the currently executing method depends, has since changed. It is normally thrown when a non-final field of a base class becomes static, or when the base class turns into an interface (and vice versa), etc.

The IncompatibleClassChangeError extends the LinkageError, which is related to problems rising from a base class that changes after the compilation of a child class.

The IncompatibleClassChangeError exists since JDK 1.0.
 

The structure of IncompatibleClassChangeError

Constructor:

The IncomatibleClassChangeError in Java

To make an example of IncomatibleClassChangeError, I will use the javac compiler from the command-line. This error is thrown when the classes are separately-compiled, and most of IDEs don’t allow this.

Create a Java class called BaseClass with the following source code:

public class BaseClass {
	public String message = "I am BaseClass";
}

To compile this class, execute this on the command-line:

>_ javac BaseClass.java

Now that the first class is compiled, create the second class, called ChildClass, with this source code:

public class ChildClass extends BaseClass {
	
	public static void main(String[] args) {
		ChildClass obj = new ChildClass();
		System.out.println(obj.message);
	}
}

The output of compiling and running this would be:

>_ javac ChildClass.java
>_ java ChildClass

>_ I am BaseClass

Now everything works just fine, but now try to change the message to static, recompile the BaseClass and rerun the ChildClass. The new BaseClass is this:

public class BaseClass {
	public static String message = "I am BaseClass";
	
}

Recompiling BaseClass doesn’t raise any problem, but when I try to run ChildClass, I get the following message:

Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected non-static field ChildClass.message
        at ChildClass.main(ChildClass.java:5)

Now let’s try another case of IncompatibleClassChangeError, that of changing the class to an interface. Change the BaseClass to an interface, with the following source code:

public interface BaseClass {}

You can see that now the BaseClass is a marker interface. It compiles normally, but when I run the ChildClass, I get the following message:

Exception in thread "main" java.lang.IncompatibleClassChangeError: class ChildCl
ass has interface BaseClass as super class
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

More about the IncompatibleClassChangeError in Java

IncompatibleClassChangeError is thrown when the programmer:

  • extends an interface
  • implements a class
  • a non-final non-static field of the base class becomes static
  • a non-final static field becomes non-static

Normally, the compiler would fail to compile the class when one of these occurs, but the IncompatibleClassChangeError is thrown when the base class is recompiled without changing the derived (i.e. child) class.

How to deal with IncompatibleClassChangeError

When you face this error, all you need to do is change and recompile the derived class according to the changes in the base class. This means, implementing instead of extending (or vice-versa), calling fields as static or non-static as they are in the base class.

Download Code

Download
You can download the full source code of this example here : IncompatibleClassChangeErrorExample

Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button