简体   繁体   English

Java ClassCircularityError,运行时还是编译时?

[英]Java ClassCircularityError , runtime or compile time?

I am reading the JVM specification 2nd edition. 我正在阅读JVM规范第二版。 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. 在本节( 正在加载 )中,将ClassCircularityError定义为如果类是其自身的超类或超接口而无法加载时抛出。 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? 我无法想像/描绘它,因为当我编译这样的类时,java编译器使我停在那里并且不让我继续。那么如何创建这样的.class进行加载?

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. 在Java中,编译单元是类,因此很有可能在不同的时间编译了这些类,例如,因为这些类由于位于单独的库中而被独立开发。 Consider: 考虑:

class A {}

Somebody else then 然后有人

class B {}

Now, the makers of A decide that they want to extend B: 现在,A的制造者决定要扩展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: 而B的制造者决定他们要扩展A,但不知道前一个更改,即仍然具有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. 后来,他们意识到可以使用A的新版本,并将其放在类路径中,从而在加载时导致ClassCircularityError。

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. 这就是为什么JVM也要对其进行检查的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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