简体   繁体   中英

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.

I have a program like this:

class A {

  static final class B {

       public int addMe() {
       }

  }

}

how do I call the method addMe ?

When I do ABaddme() I get an error.

addme() is an instance method of the inner class B. So you need a B instance to be able to call it:

A.B b = new A.B();
b.addme();

Your addMe() method is not static, so you can't call it without B class instances:

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.

You have to instantiate the static class B and call the method or mark the method itself as static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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