简体   繁体   中英

Java ClassCircularityError , runtime or compile time?

I am reading the JVM specification 2nd edition. In this section ( Loading ), ClassCircularityError is defined to be thrown when a class couldn't be loaded if it is its own superclass or superinterface. I couldn't figure/picture it as when I compile such a class the java compiler stops me there and won't let me proceed.Then how such a .class can be created to load?

public class TestCycle extends TestCycle{
private String memberVar;
}

Can anyone explain this to my jar head please?

In Java, the unit of compilation is the class, so it is quite possible for the classes to have been compiled at different times, for instance because that classes are developped indepedently due to being in separate libraries. Consider:

class A {}

Somebody else then

class B {}

Now, the makers of A decide that they want to extend B:

class A extends B {}

and the makers of B decide that they want to extends A, but are unaware of the former change, ie the still have the original definition of A:

class B extends A{}

Later, they realise a new version of A is available, and put that in the class path, resulting in a ClassCircularityError at load time.

You can create such a class file by directly editing class files (I mean if you really want to). So although it doesn't compile, a valid classfile can be created including such a class. That's why JVM checks it as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM