2

I did not find my answer in the questions I searched , that's why I am asking it .

class outer 
{
class inner  // non static .
{
}
}

For creating object of inner class I am unable to understand the logic of the part to the right of assignment operator .

outer o = new outer () ;
outer.inner y = o. new inner () ; //  I have doubt in this line .

Here outer.inner is the return type but what about the right portion ?

Thing I know : We cant write outer.inner y = new outer.inner () ; since inner is non static .

shikhar
  • 135
  • 1
  • 9

2 Answers2

0
outer o = new outer () ;
outer.inner y = o. new inner () ; 

So, the first portion outer.inner is the return type as you've stated.

Second, the o is a reference to the outer object which contains the inner object, you will only access the inner object if you have a reference to the outer object. now o. new inner () ; simply says "go into the object o, then create an object of type inner".

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
-1

The problem is that a non-static inner class is bound to a specific instance of the containing class. It's like if the class definition has an hidden reference member, sort of:

class Outer {
  class Inner {
    private final Outer outer;
    Inner(Outer outer) { this.outer = outer; }
  }
}

That's what is happening outer.inner y = o.new inner(), since inner is not static it requires an outer to be instantiated and that syntax is used to pass the hidden outer reference to the instance.

If Inner becomes static then it doesn't need a qualifying instance anymore, the name of the outer class is enough, eg: new Outer.Inner().

Jack
  • 131,802
  • 30
  • 241
  • 343
  • If `Inner` were `static` it would not be an inner class and thus the class name would be hopelessly confusing. – Lew Bloch Apr 02 '17 at 17:35
  • @LewBloch: that's just terminology, it would be a nested class instead but that's just a sophism. I don't know why they had to separate inner from nested when the static keyword already does it. – Jack Apr 02 '17 at 17:40
  • 1
    Whatever your personal opinion or the pejoratives you apply, the fact (!) is that an inner class in Java is a nested class that is not `static`. That isn't a "sophism", that's the Java Language Specification. I advise you to get over your opinion, @Jack, and deal with the facts. – Lew Bloch Apr 02 '17 at 17:49
  • @LewBloch: It's a sophism to choose to use two different synonyms (!) to distinguish a `static` nested class from a non-`static` inner class. It's a way to say "we have to find a different terminology for it at all costs", especially because `static` already imply this kind of semantics. What they chose is not a fact, it's just a valid personal opinion as mine but from a committee that decided the standard, which makes your argument totally futile. The arguments for this naming choice are debatable, regardless if "they are facts" or not. – Jack Apr 03 '17 at 16:21
  • I don't have an "argument". Whatever your personal opinion, it is a fact that the JLS defines these terms. Read it for yourself, @Jack, and confirm that those definitions are there. Given that the terms of the language are actually defined by the language's defining document, you can either choose to use the actual terms of art so defined, or to be obstinate. Since the defining document defines the terms, the language and its community will ignore your opposition with sublime indifference. – Lew Bloch Apr 04 '17 at 08:49