File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explaination reg inner classes

 
Divya Gehlot
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any one explain me below program as what is haapening and why did we used this to call flag and whats the use of using that .
 
Deepak Jain
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As you know inner class can access the instance variables of the outer class. There are two ways to access a instance variable from within a inner class.
First:
if (Test4.this.flag)
Second:
if (flag)

Second method access flag directly because inner classes can access the outer class instance members, Here inner classes implicit access to the this object of outer class, this is because inner class instance cannot exists wihtout an outer class instance, so when an inner class is instantiated compiler will implicitly pass the outer class instance to it which was used to create the inner class instance.
Second is preffered way to do so.

First one is also legal here you explicitly say this object of class Test4.
Thanks
Deepak
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic