Can someone explain the accessibility modifiers for classes ( top level, inner, local, anonymous) ie the modifiers that can be applied to the class itself ....I just want to remember them as easily as possible thank you
thanks for the replies Here are a few notes of mine for Inner Classes - Inner classes (local classes defined in a method) can have only abstract / final modifiers. - Member classes (static and non-static) can have private, protected, public, abstract, final, static, strictfp modifiers - An member class cannot have the same name as the enclosing class. - Anonymous class may extend a class (can pass params to the constructor of superclass) or implement an interface(no params can be passed), but may not be declared to do both. - Semicolons are needed at the end of the declarations of the anonymous classes. - Anonymous inner classes are declared and instantiated at the same place. - OuterClass.this refers to the Outer class reference - (OuterClass.super.x) == ((super class) OuterClass.this).x where x is a variable in the super class of OuterClass - Member inner classes can be forward referenced. Local inner classes cannot be. - Two inner class siblings can extend each other, but to extend a nested inner class(under each of the siblings) you have to used fully qualified name - Note that if the anonymous class implements an interface, then the instance creation expression can not include any parameters. - Outer.Inner im = new Outer.Inner().new InnerMost(); --> static nested class instance creation - Outer.Inner im = new Outer().new Inner().new InnerMost(); --> inner class instance creation - A class which is defined inside a method can have access to all final vars inside the same method where it is declared, and all of the variables of the enclosing class WHICH ARE AVAILABLE to the ENCLOSING METHOD (a static local inner class cannot access enclosing class's non static members) - An anonymous class can implement only one interface or can extend only one class. - A non-static inner class can not declare a static member unless it is a compile time constant. - An anonymous class is not permitted to declare a constructor, but can have instance initializers. - Member classes have access to the private fields and methods of the enclosing class and other member classes.
I've drawn the following diagram which may be useful. The terms "nested" and "inner" are used as per the JLS. For completeness: A top level class is a class that is not a nested class. These are the modifiers which can be applied to a top level class: public, abstract, final and strictfp.
[ April 25, 2003: Message edited by: Roger Chung-Wee ]
The scheme of Roger makes it look as if an anonymous inner class can only be method local. However an anonymous inner class can be assigned to a member varibale of any class: top level or inner. The Comparator interface is often implemented in the same class as the parent class, because it is only valid within the context of that class. For example in class Person:
Now we can pass the Person Comparator as parameter to a method invocation like: Arrays.sort(persons, Person.firstNameComparator);
What is strictfp modifier?And what is the difference between Nested class & inner class? Thanks veena
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
posted 15 years ago
Yes, I'd misread anonymous classes, maybe because I've only ever used them inside methods. My understanding is now as follows. An inner, ie non-static nested class, can be a member (defined inside the outer class and accessible from outside, but not inside a method) or a non-member. If a non-member, it can be method-local or anonymous. If method-local, it can be local or anonymous. Here is a revised diagram.