• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Local Inner Class Instantiation

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, don't know if this is the right place for such a question.

If I want to instantiate an ordinary Inner Class (not Local), from inside the Outer Class, I can just type:

InnerClass innerObject = new InnerClass().

This is fine, but I can also type, to underline the fact that the inner object lives inside an Outer Class instance:

InnerClass innerObject = this.new InnerClass().

This is fine too.

Now, it turns out that this second syntax does not work for Local Inner Classes (inside a method), I get an "identifier not found" (or kind of) error. If I just omit "this." everything works fine.
Any idea why for Local Inner Classes it is not possible to use the "this" reference?

Thanks,
Bye
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Valentino Verdi wrote:Hi guys, don't know if this is the right place for such a question.

If I want to instantiate an ordinary Inner Class (not Local), from inside the Outer Class, I can just type:

InnerClass innerObject = new InnerClass().

This is fine, but I can also type, to underline the fact that the inner object lives inside an Outer Class instance:

InnerClass innerObject = this.new InnerClass().

This is fine too.

Now, it turns out that this second syntax does not work for Local Inner Classes (inside a method), I get an "identifier not found" (or kind of) error. If I just omit "this." everything works fine.
Any idea why for Local Inner Classes it is not possible to use the "this" reference?



as much as i concern method local inner classes can not be instantiated outside methods..
and these classes are local to that method that`s why you can`t use "this" in that case..
hope this will be helpful.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Valentino Verdi wrote:Hi guys, don't know if this is the right place for such a question.

If I want to instantiate an ordinary Inner Class (not Local), from inside the Outer Class, I can just type:
InnerClass innerObject = new InnerClass().
This is fine, but I can also type, to underline the fact that the inner object lives inside an Outer Class instance:
InnerClass innerObject = this.new InnerClass().
This is fine too.


Yeah, but not always. It doesn't work, if these instances are created within a static method. Try this

within a static method. Doesn't work.

Valentino Verdi wrote:
Now, it turns out that this second syntax does not work for Local Inner Classes (inside a method), I get an "identifier not found" (or kind of) error. If I just omit "this." everything works fine.
Any idea why for Local Inner Classes it is not possible to use the "this" reference?


Check the last line in the local inner method. this refered here to the enclosing class object. And you can't access the (method-)local inner class from outside the method. Therefore ERROR.



Hope I could help.
Bob
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say this



Then the compiler will try to find a class named LocalInner inside the enclosing class. So this would work



But this will not work as there is no class named LocalInner inside the class in which the method is declared



And there is a funny part to this, try this

 
Valentino Verdi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all, really appreciate your help.

So we could say the following:

Even if the syntax with "this." is not allowed for instantiating a local inner class,
the local inner class instance still lives inside "this" (the outer class instance).

Bye
 
reply
    Bookmark Topic Watch Topic
  • New Topic