wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explaination reg inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Explaination reg inner classes " Watch "Explaination reg inner classes " New topic
Author

Explaination reg inner classes

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
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 .


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
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


SCJP, SCWCD, SCBCD
 
GeeCON Prague 2014
 
subject: Explaination reg inner classes