简体   繁体   中英

Inner private class and public method

If I have something like this:

public class Outer {

    private class Inner {
        public void someMethod() {}
    }

    Inner in = new Inner();
    in.someMethod(); //What??
}

Why doesn't last line code work?

Why doesn't last line code work?

Because it has to be wrapped within a block ( initializer , constructor , method ).

For example (wrapper within a constructor):

public class Outer {

    private class Inner {
        public void someMethod() {}
    }

    public Outer() {
        Inner in = new Inner();
        in.someMethod();
    }

}

You just cant invoke any method directly from class body. It has to be a method or constructor or initilizer block

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