简体   繁体   English

Java编译器为什么不能从另一个文件中找到类?

[英]Why doesn't Java compiler find class from another file?

So, Main.java calls class House from House.java , one of the methods of House.class is supposed call a method from class Appointment extends Succession , which is located in Succession.java . 所以,Main.java呼吁从House.javaHouse.class的方法一类房子应该从呼吁任命类的方法扩展继承 ,它位于Succession.java。

But for some reason, the compiler doesn't find Appointment and assumes it must be a missing variable, why? 但是由于某种原因,编译器找不到Appointment并假定它必须是缺少的变量,为什么? If I move the Appointment-class to Main.java or give it its own file, the compiler finds it. 如果我将约会类移到Main.java或给它自己的文件,编译器会找到它。

I'm asking this because, I tried re-creating the error, by I haven't been able to... 我之所以这样问,是因为我无法重新创建错误,

House.java: House.java:

public void succession(){
    if (this.kinsmen.size() > 0){
        Appointment.callHeir(this.head, Main.human.get(1));

Succession.java: Succession.java:

class Appointment extends Succession {
    public static boolean callHeir(Human appointer, Human appointed){

Error: 错误:

 error: cannot find symbol
            Appointment.callHeir(this.head, Main.human.get(1));
            ^
  symbol:   variable Appointment
  location: class House

Edit full code minimized: 编辑完整的代码最小化:

Main.java: Main.java:

class Main {
    public static void main(String[] args) {
        House.succession();
    }
}

House.java: House.java:

public class House {
    public static void succession() {
        Appointment.callHeir();
    }
}

Succession.java: Succession.java:

class Succession{

}

class Appointment extends Succession {
    public static void callHeir(){
    }
}

I'm not sure but this might be the reason: 我不确定,但这可能是原因:

When compiling Main.java it encounters House.succession() . 编译Main.java时遇到House.succession()
It doesn't find House.class so it looks for and compiles House.java . 它没有找到House.class所以它查找并编译了House.java

When compiling House.java it encounters Appointment.callHeir() . 编译House.java时遇到Appointment.callHeir()
It doesn't find Appointment.class so it looks for Appointment.java which doesn't exist. 它找不到Appointment.class因此它查找不存在的Appointment.java

When I change House.java like this 当我像这样更改House.java

public class House {
    public static void succession() {
        Succession dummy;
        Appointment.callHeir();
    }
}

everything works fine. 一切正常。
This because it encounters Succession dummy first which makes the compiler look for Succession.java . 这是因为它首先遇到了Succession dummy ,这使编译器查找了Succession.java Compiling that file, we automatically obtain Appointment.class . 编译该文件后,我们将自动获得Appointment.class

As already suggested by others, I strongly recommend to put the Appointment class in its own file. 正如其他人已经建议的那样,我强烈建议将Appointment类放在其自己的文件中。

Set Access Modifier/specifier of Appointment class as public . 将约会类的访问修饰符/说明符设置为public Because by default access modifier/specifier of any class is default which doesnot allow access the class from another class. 因为任何类的默认访问修饰符/符是default其不列入允许访问从另一个类的类。

Make Sure if you need to access the class from outside the class the access modifier/specifier of class will be public. 确保如果您需要从类外部访问该类,则该类的访问修饰符/说明符将是公共的。

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

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