tags:

views:

638

answers:

2

Hi, I have the following java class:

class Outer
{
    private Integer a;
    private Long b;

    class Inner
    {
        public void foo()
        { 
            System.out.println("a and b are " + a + " " + b);
        }
    }
}

when I run javap on Outer and Outer$Inner, I get the following:

C:\test>javap Outer
Compiled from "Outer.java"
class Outer extends java.lang.Object{
    Outer();
    static java.lang.Integer access$000(Outer);
    static java.lang.Long access$100(Outer);
}

C:\test>javap Outer$Inner
Compiled from "Outer.java"
class Outer$Inner extends java.lang.Object{    
    final Outer this$0;
    Outer$Inner(Outer);
    public void foo();
}

I have two questions:

1) why does java compiler generate static methods that take 'Outer' param, in the outer class, for accessing its private variables ? why not instance methods that the inner class can easily call through its this$0 member ?

2) why is this$0 in inner class made final ? what will happen if it is not final ?

Thanks and regards.

+4  A: 

Non-static inner classes have an implicit reference to an instance of the outer class. This is implemented as a final reference to the outer class. If it wasn't final technically it could be modified after instantiation.

The outer class is implicitly passed in which is why any constructors on the inner class have an implicit parameter of the outer class, which is how this$0 is passed in.

Edit: as for the access$000 methods the key clue is that they're package access and they take an Outer as an argument. So when code in Inner calls, say, Inner.this.a it's actually calling Inner.access$000(this$0). So those methods are there to give access to private members of the outer class to the inner class.

cletus
why the `access$...()` methods need to be `static`? The answer you provided is for the second question, and for the question that is never asked.
Adeel Ansari
@Vinegar: see update.
cletus
Hi Cletus, thanks for your answer. I understand that 'access*' methods are given so inner class can access private members of outer class. But why are these methods static ? why can't they be instance methods ? since inner class has this$0, the code there can as easily call this$0.access$000 had it been an instance method, instead of calling Outer.access$000(this$0).
shrini1000
If they weren't static then you'd have to worry about every potential subclass accidentally overriding them. Only static methods provide the class-specific semantics required.
PSpeed
Vinegar's response explains that better... I should have scrolled down.
PSpeed
Thanks Cletus, for the update.
Adeel Ansari
+2  A: 

1) They have to be static, in order to not be overridden in some sub class. I hope you understand.

<Addendum>

Shrini, from your comment, it seems that there is a need to explain the things to avoid some misconceptions. First of all, know on thing that static methods can not be overridden. Overriding is exclusive in objects, and it is there to facilitate polymorphism. Whereas static methods belongs to the class. Found a couple of good resources to support my argument and for you to understand that static methods can not be overridden.

Now for your second retort, you are right in saying that they have package level access and can't be overridden in subclasses outside the package. But I don't know why are you neglecting the case where subclass/es exist in the same package. Its a valid case, IMO. In fact, it would be absurd to name a method like access$000() or something like that, in a real work. But don't underestimate the chance of accidental overriding. There can be a case where the subclass of Outer, say SubOuter, has an inner class of itself too. I didn't try to javap that case myself, just guessing.

</Addendum>

2) Even if you think it will not get modified, technically there is a possibility as cletus pointed out already, use of final can provide easy optimizations by the compiler.

Adeel Ansari
Hi Vinegar, thanks for your answer. But subclasses can override their parent classes' static methods; plus in this specific case the methods are package access, so subclasses outside of this package won't be able to override them anyway. So I don't understand #1 of your answer.
shrini1000
@shrini1000: No, you are in a folly. See my addendum.
Adeel Ansari
Hi Vinegar, thank you for your explanation. I had coded a super/sub class and tested it before making my earlier reply; but I hadn't considered the case where a superclass ref points to a subclassed object. Thanks for pointing that out.And my apologies if my reply offended you; that was never the intention.
shrini1000