• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

accessibility modifiers for classes

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see Section 8.1.1 of the Java Language Specification.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chk out this link :
Modifiers
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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, coderanch, 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.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: coderanch, abstract, final and strictfp.

[ April 25, 2003: Message edited by: Roger Chung-Wee ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Semicolons are needed at the end of the declarations of the anonymous classes.


Not always:
method( new Cloneable() { } );
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Member classes (static and non-static) can have private, protected, coderanch, abstract, final, static, strictfp modifiers


Only static member classes can be declared static (see my diagram).
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is strictfp modifier?And what is the difference between Nested class & inner class?
Thanks
veena
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic