[英]How can a class in a package use other classes in the same package's static methods?
I have classes A and C in package abc. 我在abc包中有A和C类。 A has a static method showA(). A具有静态方法showA()。 Now I want to use this method in C.How do I do this? 现在我想在C语言中使用此方法。
package abc;
public class A{
public void static showA()
System.out.println("I am in A");
}
}
package abc;
public class C{
public void static showC(){
A.showA();
System.out.println("I am in C");
}
}
Now while compiling C it shows that, cannot find variable A. How to resolve this? 现在,在编译C时,它表明找不到变量A。如何解决此问题?
You didn't give exact information about what you did, but I fear that you are compiling the classes one by one with calls like 您没有提供有关您所做操作的确切信息,但是我担心您会像调用这样逐一编译类
javac abc/A.java
javac abc/B.java
You have 2 possibilities: The first one is to tell the compiler to compile both classes. 您有两种可能性:第一种是告诉编译器编译两个类。 That way both classes will be known: 这样,两个类都将被知道:
javac abc/A.java abc/B.java
Another possibility is to tell the compiler where the required class file can be found. 另一种可能性是告诉编译器在哪里可以找到所需的类文件。 As A.Java is compiled to A.class with the same base directory, you could do the calls: 由于将A.Java编译到具有相同基本目录的A.class,因此可以执行以下调用:
javac abc/A.java
javac -cp . abc/B.java
With -cp you add the local directory to the classpath so A.class is on the classpath. 使用-cp可以将本地目录添加到类路径,以便A.class在类路径上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.