Java 没有类似未定义类的错误

Java 没有类似未定义类的错误,java,compilation,Java,Compilation,我有以下二叉树的起始代码。我只是想知道,BinaryTree类是否还没有在A行和B行定义。为什么我没有在A行和B行得到编译错误报告-BinaryTree没有定义。 我假设,C行是类被完全定义的地方 public class BinaryTree { private int data; private BinaryTree left; // Line A private BinaryTree right; // Line B public BinaryTree(int num) { this

我有以下二叉树的起始代码。我只是想知道,
BinaryTree
类是否还没有在A行和B行定义。为什么我没有在A行和B行得到编译错误报告-
BinaryTree没有定义
。 我假设,C行是类被完全定义的地方

public class BinaryTree {
private int data;
private BinaryTree left;  // Line A
private BinaryTree right;  // Line B

public BinaryTree(int num) {
this.data = num;
this.left = null;
this.right = null;
}
// getters and setters.
}  // Line C

为了扩展tkausl的评论,下面是关于范围的Java语言规范:

具体而言:

顶级类型(§7.6)的范围是声明顶级类型的包中的所有类型声明

如果你跳到第7.6节,甚至有一个例子可以回答你的问题:

示例7.6-2。顶级类型的范围

package points;
class Point {
    int x, y;           // coordinates
    PointColor color;   // color of this point
    Point next;         // next point with this color
    static int nPoints;
}
class PointColor {
    Point first;        // first point with this color
    PointColor(int color) { this.color = color; }
    private int color;  // color components
}
这个程序定义了两个类,它们在类成员的声明中相互使用。由于类类型Point和PointColor的作用域是包点中的所有类型声明,包括当前编译单元中的所有类型声明,因此此程序可以正确编译。也就是说,向前参考不是问题

为了完整起见,请转发参考(我也必须查找此项…):


<>爪哇> StdRe<强>:java规范说这应该是OK,是.< /P>在C++中没有必要,在C++中我们需要头文件。这是全字的完整代码吗?这个代码编译得很好。(就其本身而言,只为程序添加了一个
main()
方法。)您是否有一个示例演示您所描述的问题?