5

I took the following code from the K&B book "SCJP Sun Certified Programmer for Java 6 Study Guide":

class A { // 1
    void m() { 
        System.out.println("outer"); 
    } 
}

public class TestInners {

    public static void main(String[] args) {

        new TestInners().go();

    }

    void go() {

        new A().m();

        class A { // 2
            void m() { 
                System.out.println("inner"); 
            } 
        }

    }

    class A { // 3
        void m() { 
            System.out.println("middle"); 
        } 
    }
}

As stated in the book, this code prints "middle". I infer that the class declaration marked as "3" is shadowing the one marked as "1", which is external to TestInners class. If the classes were in different packages, I could resolve the ambiguity by qualifying one of them with the package name. But in this case the classes are not only in the same package but in the same file. How can I get an instance of the external class?

I saw the same question here but the accepted answer implies to modify the code adding an enclosing class to the whole thing. My question is how to get the instance using any type of qualifier or reference, if it's even possible.

Community
  • 1
  • 1
DiegoAlfonso
  • 237
  • 5
  • 15
  • 3
    If they are in _any_ package, say 'p', you can use 'p.A' for //1 and `p.TestInners.A' for //3. Otherwise I don't think that it's possible to refer to them explicitly. Since you'll have to violate the coding standards to get to this point (class without package), this is not an issue in practice, though. – creichen Nov 28 '13 at 15:51

3 Answers3

4

Assuming your class is in package com.test, all you need to do is use

new com.test.A().m();

using the fully qualified name of the class.

If your classes are in the default package, ie. no package declaration, then you are out of luck and can't access the outer A.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • *If the classes were in different packages, I could resolve the ambiguity by qualifying one of them with the package name* OP already knows this. Re-read the question: *But in this case the classes are not only in the same package but in the same file. How can I get an instance of the external class?* – Luiggi Mendoza Nov 28 '13 at 15:52
  • 3
    @LuiggiMendoza: I think the point is that even if they're in the *same* package (and file), this would work. – Oliver Charlesworth Nov 28 '13 at 15:53
  • 1
    @OliCharlesworth but he does not have a package. – Domi Nov 28 '13 at 15:54
  • @Domi: Perhaps. But that should never be the case in practice. – Oliver Charlesworth Nov 28 '13 at 15:54
  • @LuiggiMendoza the fully qualified name of `//3`is `.TestInners.A`, so `.A` should access the outer class, while the call you are making is only accessing the "nearest" class. (packagewise) – LuigiEdlCarno Nov 28 '13 at 15:55
  • @LuigiEdlCarno but for this example, the class isn't inside a package. I know that having classes in default package is not practical, but that's the code of the question. IMO the only answer would be [creichen's comment](http://stackoverflow.com/questions/20270183/java-inner-class-shadowing-external-class/20270263?noredirect=1#comment30237847_20270183). – Luiggi Mendoza Nov 28 '13 at 15:57
2

In C++, you can explicitly address global scope by prefixing your symbol with ::, however, Java does not have such a thing.

So if you really want to get the outer A, you have to bite the bullet and do some other sort of enclosure, by for example wrapping it in another class or package.

EDIT: Here is another reason why.

Community
  • 1
  • 1
Domi
  • 22,151
  • 15
  • 92
  • 122
0

object of innner-A can't be created before defining it.so use new A().m(); after define innner-A inside go() to access inner class object.

void go() {

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

to access outer-A you have to append package name,in default package it is impossible to access outer-A.