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)
posted 8 years ago
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