Question

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 ?

Was it helpful?

Solution 2

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();    

    }    
}

OTHER TIPS

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top