Hi, Can any of you clearly list the limitations /accessibility of static modifiers & variables? Let me make it more clear in the exam point of view. While going through a code what important aspects about static modifiers & variables should we be alert of??? This is kind of important. Thanks
Hi, This is what I know : When you apply static modifier for a member of a class, it means that the member is not associated with a particular instance of the class it is contained. Rather, it is associated with the class itself. static modifier can be applied to : 1. Inner class 2. Methods 3. Variables 4. Free floating blocks 1. static inner classes : a. Cannot access instance variables of outer class. b. Are not associated with instance of outer class unlike nonstatic inner classes. c. Are as good as top level classes with modified naming syntax. d. Can contain static variables unlike nonstatic inner classes. 2. static methods : a. Cannot access instance variables of the class. b. Do not have "this" reference. c. I think they don't have "super" reference either. d. Cannot be overridden to be nonstatic. e. Do not participate in dynamic binding. f. Can contain a nonstatic/static inner class. g. Cannot be abstract.
3. static variables : a. Initialized when the class is loaded into JVM. b. Are shared by all instances of the class. 4. static blocks : a. Are executed only once : when the class is loaded into JVM. b. Can access only static methods and variables. c. Are executed in the order of their definition in the class. You have asked to list the aspects of static modifier from exam point of view. Actually I am yet to take SCJP. But I think we should know all these points. We can understand all these points well if we do some experiments with them. Well, that is all I know. Anybody, please correct me if I am wrong. Add any other details you know. Thanks Milind
I have doubt about the following: 2. static methods : f. Can contain a nonstatic/static inner class. Can a static method contain a static inner class? I don't think so. Static inner class can only be member class.They can not be defined in a method.