Aldo Ziflaj

About 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 a developer for Mozilla Albania team.

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close