简体   繁体   English

Java中的其他类无法调用方法

[英]Unable to call methods from other classes in Java

  1. When using Windows cmd (and Notepad++) i am able to call methods from other classes if they are all in default package当使用 Windows cmd(和 Notepad++)时,如果它们都在默认 package 中,我可以调用其他类的方法

  2. However, I am unable to do so if i define classes in any package other than default package.但是,如果我在除默认 package 之外的任何 package 中定义类,我将无法这样做。

  3. Am facing this problem in NetBeans and Eclipse as well我在 NetBeans 和 Eclipse 也面临这个问题

Any help is appreciated任何帮助表示赞赏

If you are defining class in package-A, it must be public , if you want to use it in package-B如果你在 package-A 中定义 class ,它必须是public ,如果你想在 package-B 中使用它

package package-A;
public class A{
    public void hello();
}

package package-B;
public class B{
    public static void main(String[] args){
        A a = new A();
        a.hello();
    }

}

Because by-default access-modifier is default , so it will be accessable in that particular package only.因为默认访问修饰符是default ,所以它只能在特定的 package 中访问。

To learn more about access modifiers: Click Here要了解有关访问修饰符的更多信息:单击此处

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

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