File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Explaination reg inner classes

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
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
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.
if (Test4.this.flag)
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.

I agree. Here's the link:
subject: Explaination reg inner classes
It's not a secret anymore!