Hi folks, I have two questions regarding inner classes. 1. I understand that a Non-Static Inner Class (Member class), cannot have static members as it cannot exist without an outer instance hence it cannot provide service such as static members. But why can't static local classes define static members? What is the underlying logic that tells us that static local classes can't define static members. When they can infact exist without an outer instance! 2. Non-static inner classes (Member classes) or Local classes cannot have static members however they can extend classes having static members. In this case won't these static variables be inherited in the member classes / local classes. Then these member classes/ local classes would have existing static members? or?? Can anyone discuss? thanks, Zulfiqar Raza
Hi, 1.Can you define a static variable inside a method(both static & non-static) ? It is not possible. Like that you cannot have static varibles defined in side local classes which are inside a method. 2. Why non-static inner class connot have static mebers ? Because the static member is always belongs to class not to the instance. Here the non-static inner class itself belong to an "instance" of outer class. Then how can you define a static variable inside. But it is possible inside static inner class since this class does not tied with any instance. I hope it is clear. solaiappan
Hi Zulfiqar, 1. The logic seems to be that local inner classes are not members and the static modifier can only be applied to members.
... local inner classes are not members of the class...therefore they cannot be private, protected, public or static .. because these modifiers apply only to class members. The Java Programming Language: Second Edtion pg 53
2. Local inner classes can inherit static members of the class they extend.
The above compiles and, when run, prints: Static var: 10. So local inner classes behave as you'd expect re: inheritance. Hope that helps. ------------------ Jane The cure for boredom is curiosity. There is no cure for curiousity. -- Dorothy Parker