Add to Google Reader or Homepage

java.lang.incompatibleclasschangeerror

Incompatible class change error:


If you read the error message Incompatibleclasschangeerror,you may understand that a class had been incompatibly changed. But you may have no idea about what causes this exception to be thrown?.Here i have presented you,some simple situations at which this exception will be thrown.Lets have a look at it.

We may all know about the binary compatibility which must be preserved when we use third party libraries in our application.

Binary Compatibility- what does it mean?


Whenever we change an application(especially a class)we should ensure that the newly modified class is compatible with pre-existing binaries(class) otherwise,you would suffer from a linkage error.

I have explained three simple reason at which this exception may arouse.

Reason 1:


Lets have two classes like class A and class B as follows.

Class A:

public class A
{
int t=10;
}

Class B:

class B
{
public static void main(String args[])
{
A r=new A();
r.t=20;
}
}

output:


Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected non-
static field A.t
        at B.main(B.java:6)


If you compile and execute the class B everything will work fine and you will not get any error.Now let's change the field t (int t=10) in class A into a static field and compile the class A alone. Now if you execute the class B(without recompiling it) then a java.lang. incompatibleclass change error would be thrown.

Thus if you have compiled the classes as a whole then at the compile-time itself you would be able to identify this problem.

"Note that if you change a public or protected variable from static to non-static or vice-versa then this exception will be thrown".

Reason 2:


This limitation is not only confined to variables,it is also applicable to methods as well.Thus if you try to change a public or protected non-static method into a static method or vice-versa,at 
that time also this exception will be thrown.

Class A:


public class A
{
public static void display()
{
int t=10;
}
}

Class B:


class B
{
public static void main(String args[])
{
A r=new A();
r.display();
}
}

Output:

Exception in thread "main" java.lang.IncompatibleClassChangeError: Expecting non
-static method A.display()V
        at B.main(B.java:6)

Reason 3:


Before explaining this reason i would like to remind you all about an important thing that "whenver you add a field to an interface it may hide the field inherited from the super class ".So if you declare same variable of same type in both the interface and in the super class then this incompatible error would be thrown. Look at the following example, I have used the classes A,B,C,D and an interface Incmp as follows.


Incmp:

interface Incmp
{
//C c=new C("10");
}


Class A:

public class A
{
C c=new C("ganesh");
}

Class B:

class B extends A implements Incmp
{
public void cal()
{
Object cobject=c;
c.display();
}
}

Class C:

class C
{
String c;
public C(String s)
{
c=s;
}
public void display()
{
System.out.println(c);
}
}

Class D:

class D
{
public static void main(String args[])
{
B b=new B();
b.cal();
}
}

As you see the interface Incmp  i made those those statements as comment lines and when i execute it the everything works fine.When i remove the comment lines and compile the interface alone then this error is thrown when i execute the class D.

Output:


C:\blog>javac D.java

C:\blog>java D
ganesh

C:\blog>java D
Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected non-
static field B.c
        at B.cal(B.java:5)
        at D.main(D.java:6)


It may also occur at some other situations say for example,

we know that to extend a class we need to include the extends keyword,if you use implements  keyword instead of it or vice-versa then also this exception would be thrown.Though this reason looks silly,if you have accidentally done like that,then you would be facing this error.

Note:


1.Usually this kind of  awkward situation occurs when we use third-party libraries in our application, because the new version may have changed the signature of the variable or method that exists in the older package.So in order to avoid these kind of situations read the documentation of the package fully,before using it.

2.I have also encountered this error and it is not identified at compile-time even if i have compiled the classes as a whole,but it is thrown at run-time.The reason is that,while resolving symbolic references the compiler used the older version of the package and during execution JVM used the different version,since compiler and JVM has different search strategies for searching classes.

17 comments:

Chiraz Amara said...

if it occurs how to fix it? I got it when upgrading from java7 to java 8 with sun.util.resources.LocaleData.getNumberFormatData(Ljava/util/Locale;)Ljava/util/ResourceBundle

Chiraz Amara said...

to fix this, you need to update atdl4j.jar

Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Giri Mani 2 said...

This blog explains me a incompatible error. That are clearly defined with coded examples. Thanks for this blog.
Java Training in Chennai

Lenova Services said...

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing

lenovo ideapad service center chennai
lenovo laptop service center in chennai
lenovo thinkpad service center chennai

Reena S said...

found something new in this blog, thanks for sharing this valuable articles.
keep updating -

Best Core Java Training in Chennai

Xplorant Seo said...

Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

iOS Training in Chennai

Freshers Jobs News said...

Thank you for taking the time to provide us with your valuable information.
Freshers Jobs in Chennai

Priya B said...

Thank you for taking the time to provide us with your valuable information.
Java Training in Velachery
Software Training institute in Velachery

Divya M said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training

Richa T said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Java training in Chennai | Java training in Bangalore

Java interview questions and answers | Core Java interview questions and answers

Naga Manickam said...

Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
Data Science training in kalyan nagar | Data Science training in OMR | Data science training in chennai

Data Science training in chennai | Best Data science Training in Chennai | Data science training in velachery | Data Science Training in Chennai

Data science training in tambaram | Data Science training in Chennai | Data science training in jaya nagar | Data science Training in Bangalore

gkr ragini said...

Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
excel advanced excel training in bangalore | Devops Training in Chennai

Mouni yoga said...

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
python Online training in chennai
python Online training in bangalore
python interview question and answers

Mouni yoga said...

I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
python Online training in chennai
python Online training in bangalore
python interview question and answers

sathya shri said...

This is my 1st visit to your web... But I'm so impressed with your content. Good Job!
angularjs Training in marathahalli

angularjs interview questions and answers

angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in chennai

automation anywhere online Training

Vikash Kumar said...

ielts coaching in gurgaon

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top