File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes accessibility modifiers for classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "accessibility modifiers for classes" Watch "accessibility modifiers for classes" New topic
Author

accessibility modifiers for classes

preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
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
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Please see Section 8.1.1 of the Java Language Specification.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Rajeshwari Natarajan
Ranch Hand

Joined: Mar 05, 2003
Posts: 67
chk out this link :
Modifiers


regards<br />Rajeshwari. N
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
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.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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 ]

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

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

Not always:
method( new Cloneable() { } );


SCJP2. Please Indent your code using UBB Code
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Member classes (static and non-static) can have private, protected, public, abstract, final, static, strictfp modifiers

Only static member classes can be declared static (see my diagram).
John Zoetebier
Ranch Hand

Joined: Mar 28, 2003
Posts: 76
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);
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
What is strictfp modifier?And what is the difference between Nested class & inner class?
Thanks
veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessibility modifiers for classes