[英]How do I call a method which is present inside a static class?
I'm very new to Java programming and I have a doubt. 我是Java编程的新手,我对此表示怀疑。
I have a program like this: 我有一个这样的程序:
class A {
static final class B {
public int addMe() {
}
}
}
how do I call the method addMe
? 如何调用方法
addMe
?
When I do ABaddme()
I get an error. 当我执行
ABaddme()
,出现错误。
addme()
is an instance method of the inner class B. So you need a B instance to be able to call it: addme()
是内部类B的实例方法。因此,您需要一个B实例才能调用它:
A.B b = new A.B();
b.addme();
Your addMe()
method is not static, so you can't call it without B
class instances: 您的
addMe()
方法不是静态的,因此如果没有B
类实例,就无法调用它:
A.B b = new A.B();
b.addMe();
Typical java inner class (without static) can be initialized only inside the class it is declared in. A static inner class is actually a normal class, visible to any other class in the program, but declared in other class' file. 典型的Java内部类(无静态)只能在声明它的类内部进行初始化。静态内部类实际上是普通类,对于程序中的任何其他类都是可见的,但在其他类的文件中声明。
You have to instantiate the static class B and call the method or mark the method itself as static. 您必须实例化静态类B并调用该方法或将该方法本身标记为静态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.