简体   繁体   English

包中的类如何在同一包的静态方法中使用其他类?

[英]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.

相关问题 如何创建一个可执行的 jar 文件,该文件可以执行同一 package 中其他 java 类的方法 - how to create a executable jar file which can execute the methods from other java classes in same package 如何在Java中对同一包的类使用静态导入 - How to use static import on classes of same package in Java 如何在Java中创建对同一包中的其他类不可见的(私有)类? - How to make a (private) class in java that is not visible to other classes in the same package? 实现相同接口的两个类可以在其常用方法中使用其他类的对象作为参数吗? - Can two classes implementing same interface use other class's object as parameter in its common methods? 如何将我的jButtons链接到同一包中其他类中的方法 - How can i link my jButtons to methods in other class in the same package 如何在Java中的其他类中使用静态方法 - How can I use static methods in other classes in java 无法在同一个包中的编译类中看到静态方法 - Cant see static methods in compiled classes in same package 使用带有嵌套静态类的类的良好做法,然后使用更多静态方法扩展包私有抽象类以保持组织? - Good practice to use a class w/ nested static classes, that then, extend package-private abstract classes with more static methods to stay organized? java如何在其他包中使用类? - java how to use classes in other package? 类“ SynchronousHelper”的签名者信息与同一包中其他类的签名者信息不匹配 - class “SynchronousHelper”'s signer information does not match signer information of other classes in the same package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM