11
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 ?

5
  • 1
    By the way, since it is not a static class, you need a live instance of Outer to create an instance of Inner. If Inner was static, then you could do new Outer.Inner(). Commented Apr 4, 2014 at 15:12
  • 1
    @okiharaherbst This is not a duplicate of the question that you linked (it might be a duplicate of some other question, I don't know for sure). Commented Apr 4, 2014 at 15:13
  • yes i am talking about the way of creating Inner class reference ? how its possible ? Commented Apr 4, 2014 at 15:15
  • @dasblinkenlight: Look at the accepted answer there. I presume that this is the explanation sought by the OP. Commented Apr 4, 2014 at 15:15
  • 1
    @dasblinkenlight I think it is a duplicate. What you explained in your answer is, in fact, what is explained in the accepted answer of that Q/A. Commented Apr 4, 2014 at 15:22

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

Comments

4

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 Comments

still don't get the point i think :) , Outer.Inner is something like invoking a static member in Outer class ? is that ?
@KalhanoToressPamuditha Outer.Inner is the full name of the type. The syntax looks similar because Java uses dot for scope resolution as well as for member access, but it is really a different operator: in the context of a type name, it means "The class Inner inside the class Outer".
3

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.