Hello everybody. I'm new to Java but have some coding background in C. I'm aiming for a SCJP 1.4 certification. I have not been able to grasp the following concept: ****** Static local classes can access non-static members from it's superclass but cannot access non-static members from its enclosing class? ****** I've really not been able to comprehend this concept. I would appreciate if you guys could explain this to me. I'm referring to this example from Khalid Mughal's certification guide:
Hi Harwinder. Would you mind if I simplify the notation.
(1) z1 can be initialized with b1. z1 is an instance variable of Z and b1 is an instance variable of superclass B. z1 can refer to b1, because a reference to the Z object exists when the instance variables b1 and z1 are initialized. Here is why. When an instance of Z is created, memory is allocated for the instance variables b1 and z1. Therefore, a reference to the Z object on the heap exists. Then the instance variables b1 and z1 are initialized. The Z object is created and the instance variables are initialized when the expression new Z() is evaluated. (2) z1 cannot be initialized with a1 or c1 class Z is declared in a static method of class A. There is no reference to an A object in the static method. So Z cannot refer to declared or inherited instance variables of A. The key is to know, when code is executed, whether a reference to the Z object or A object exists. (Harwinder, people will be very confused if you call that class a static local class. I suggest you call it a local class declared in a static method. I suggest you change your post title, before people get focused on the wrong issue.) [ October 18, 2003: Message edited by: Marlene Miller ]
Joined: Oct 17, 2003
Hi Marlene Your explanation does make sense to me. I'll revert back in case I've more doubts. Thanks a lot for your help. (And I've changed the subject ... I understand the concern you raised) Harwinder [ October 18, 2003: Message edited by: Harwinder Bhatia ]
Joined: Mar 05, 2003
Hi Harwinder. A lot of interesting and helpful things can be said about local classes accessing variables of the enclosing class. It is difficult to be concise and helpful at the same time. Please continue to ask questions if you are not completely satisfied. [ October 18, 2003: Message edited by: Marlene Miller ]