-2

I have a sample code to solve which is based on inner classes:

package inner;

class A {
    void m() {
        System.out.println("Outer");
    }
}

public class TestInner {
    public static void main(String[] args) {
        new TestInner().go();       
    }

    private void go() {
        new A().m();
        class A{
            void m(){
                System.out.println("Inner");
            }
        }
        new A().m();
    }
    class A{
            void m(){
            System.out.println("Middle");
        }
    }
}

The output given by above sample code is:

Middle 
Inner

And my question is, given that I dont want to use the package name to create an object, how can I print the output as:

Outer
Inner
tmarwen
  • 15,750
  • 5
  • 43
  • 62
Pranit
  • 1
  • 1
  • 4
    You have 2 hours. No spelling mistakes. – sp00m Apr 20 '12 at 08:00
  • Please use boldface if and only if it's absolutely necessary from now onwards. – Lion Apr 20 '12 at 08:02
  • This is a very contrived puzzle, I don't see what's interesting about it since it's just about namespacing. You're gonna have to see quite some millions of lines of production-level Java before you encounter a second package-private class inside a compilation unit. – Marko Topolnik Apr 20 '12 at 09:13

2 Answers2

0

Since using a package is so obviously the answer, I assume you are looking for something obtuse.

You can add an outer class and call that.

class B extends A { }

// in TestInner.go()
    new B().m();
    class A{
        void m(){
            System.out.println("Inner");
        }
    }
    new A().m();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
public class TestInner {
public static void main(String[] args) {
    new TestInner().go();       
}

private void go() {
    new inner.A().m();        //will produce output "Outer"
    class A{
        void m(){
            System.out.println("Inner");
        }
    }
    new A().m();             //will produce output "Inner"
}
class A{
        void m(){
        System.out.println("Middle");
    }
}