tags:

views:

282

answers:

5
public class Test {
    public static void main(String[] args) {

    }
}

class Outer {
    void aMethod() {
        class MethodLocalInner {
            void bMethod() {
                System.out.println("Inside method-local bMethod");
            }
        }
    }
}

Can someone tell me how to print the message from bMethod? Thanks.

A: 

This might get you started, (I don't have anything handy to test with). Notice the modified constructor syntax:

http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

Sam
+4  A: 

You can only instantiate MethodLocalInner within aMethod. So do

void aMethod() {

    class MethodLocalInner {

            void bMethod() {

                    System.out.println("Inside method-local bMethod");
            }
    }

    MethodLocalInner foo = new MethodLocalInner(); // Default Constructor
    foo.bMethod();

}

Thanks..realised where I went wrong..I put the new instance line before the localinner class creation.
Omnipotent
+1  A: 

Within the method aMethod after the declaration of the class MethodLocalInner you could for instance do the following call:

new MethodLocalInner().bMethod();
Bno
+1  A: 

Why don't you just create an instance of MethodLocalInner, in aMethod, and call bMethod on the new instance?

Lior
A: 

Please read Thinking in Java