explain the way to access inner class in java? [duplicate]

不羁的心 提交于 2019-12-12 10:38:44

问题


class Outer {    
    class Inner {       

    }    
}

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

        Outer o = new Outer();
        Outer.Inner inner = o.new Inner();    

    }    
}

the way I create a reference for Inner class object is something like accessing static member in Outer class ?
could you please explain the mechanism behind this ?


回答1:


the way I create a reference for Inner class object is something like accessing static member in Outer class

Not at all - since you are using an instance of the Outer to access the constructor of the Inner, this is very much like accessing an instance member of the Outer class, not its static member. The name of the Inner class in the declaration is qualified with the name of its outer class Outer in order to avoid naming conflicts with top-level classes.

The reason for that is easy to understand: Inner is a non-static inner class, so it needs to reference an Outer. It does so implicitly, but the reference must be passed to the constructor behind the scene. Therefore, you call a constructor of Inner on an instance of Outer.

The syntax for making instances of static classes is similar to the syntax for making instances of regular classes, except the name of the nested class must be prefixed with the name of its outer class - i.e. following the static syntax:

class Outer {    
    static class Inner {       

    }    
}

public class Demo {
    public static void main(String args[]) {
        Outer.Inner inner = new Outer.Inner();    

    }    
}



回答2:


You're actually accessing an inner class in a non-static way. A static inner class is actually different - the compiler makes a top-level class for it that is hidden from the programmer, which then works similarly to the way of instantiation that you have posted for the inner class.

You have to declare this way because since the inner class is non-static, it needs an instance of the outer class to make an instance of the inner class.

Outer o = new Outer(); 

is the required instance for the outer class.

Outer.Inner inner = o.new Inner(); 

is required for the instance of the inner class.




回答3:


Not sure exactly what you are asking, but your code is valid.

You can only instantiate Inner if you have an instance of Outer, so you call only call Inner's constructor in the context of an instance of Outer, hence

Outer o = new Outer();
Inner i = o.new Inner();

works, but

Inner i = new Outer.Inner(); //bad
Inner i = Outer.new Inner(); //bad

are both trying to access Inner in a static way, and will not compile.

If you want create instances of Inner without first creating an instance of Outer, then Inner needs to be static




回答4:


The reason why this looks weird is because you declared non-static inner class. That means that inner class will have access to instance variables in the enclosing class and would have to have a this reference to it.

Think of it this way: non-static inner class is a part of the enclosing class's instance like any other field. You need to specify a particular instance in order to create an object. So just like you'd initialize a regular field like this (don't actually do that, it's bad): o.someField = new Object(), you initialize inner class the way you did.



来源:https://stackoverflow.com/questions/22866454/explain-the-way-to-access-inner-class-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!