(update) Use "default" keyword for default visibility
Adrian Kuhn
akuhn at iam.unibe.ch
Wed Mar 4 16:43:42 PST 2009
On Mar 5, 2009, at 00:28 , Joseph D. Darcy wrote:
> Not all modifiers are applicable in all places. For example,
> "private" cannot appear on the methods of an interface. A more
> convincing specification would list explicitly the grammar changes
> and updated JLS sections.
That's feels like writing up house rules for D&D :)
Changes to http://java.sun.com/docs/books/jls/download/langspec-3.0.pdf
6
[..] The default access is that a member can be accessed anywhere
within the package that contains its declaration; other possibilities
are public, protected, and private.
[* Actually, this is not true for interface members and enum
constructors! The introduction of the chapters seems to be prose
anyway, so I dont fix that for now. ]
6.6.1 second item
- [...] If a top level class or interface type is declared "package"
or has no access modifier, then it may be accessed only from within
the package in which it is declared.
6.6.1 last item
- Otherwise, if the member or constructor is declared "package" or has
no access modifier, then access is permitted only when it occurs from
within the package in which the type is declared.
6.6.4
If a class lacks the public modifier or is declared "package", access
to the class declaration is limited to the package in which it is
declared (§6.6).
[* Example may need to be updated.]
6.6.5
If the "package" modifier is specified or none of the access modifiers
are specified, [..]
If a public class has a method or constructor with default access or
"package" modifier, then this method or constructor is not accessible
to or inherited by a subclass declared outside this package.
[* Example may need to be updated.]
8
Field, method, member class, member interface, and constructor
declarations may include the access modifiers (§6.6) package, public,
protected, or private.
8.1.1
/ClassModifier: one of/
/Annotation/ package public protected private
abstract static final strictfp
[* I could not find a sentence that declares multiple access modifiers
to be a compile-time error. Where is this stated for classes if not
here?]
8.2.1.1
[* Example may need to be updated.]
8.3.1
/FieldModifier: one of/
/Annotation/ package public protected private
static final transient volatile
A compile-time error occurs [..] if a field declaration has more than
one of the access modifiers package, public, protected, and private.
8.4.3
/MethodModifier: one of/
/Annotation/ package public protected private abstract
static final synchronized native strictfp
A compile-time error occurs [..] if a method declaration has more than
one of the access modifiers package, public, protected, and private.
8.4.8
[..] of the superclass and superinterfaces that are public, protected,
"package" or declared with default access in the same package as C and
[..]
[...]
- /m2/ is public, protected, package or declared with default access
in the same package
as C, or
[...]
8.4.8.4
If the overridden or hidden method is "package" or has default access,
then the overriding or hiding method must not be private; otherwise, a
compile-time error occurs.
8.5.1
The access modifiers package, public, protected, and private are
discussed in §6.6. A compile-time error occurs if a member type
declaration has more than one of the access modifiers package, public,
protected, and private.
8.8.3
/ConstructorModifier: one of/
/Annotation/ package public protected private
The access modifiers package, public, protected, and private are
discussed in §6.6. A compile-time error occurs [..] if a constructor
declaration has more than one of the access modifiers package, public,
protected, and private.
It is a compile-time error if the constructor of an enum type (§8.9)
is declared
package, public or protected.
[* BTW, another irregularity here: for enum constructors, no access
modifier means private not default access. This may violate the
principle of least surprise. ]
8.8.9 5th paragraph
[..] otherwise, if the class is declared "package" or has no access
modifier, the default constructor is implicitly given the access
modifier "package".
9.1.1
/InterfaceModifier: one of/
/Annotation/ package public protected private
abstract static strictfp
[* Again, I could not find a sentence that declares multiple access
modifiers to be a compile-time error. Where is this stated for
interfaces if not here?]
9.3
[* Would need a change if we want to allow interfaces members to be
package visible. See Alex Buckley's blog posts. Currently, if
interface members do have no access modifiers, they are assumed to be
public. ]
9.4
[* Same as above.]
12.3.3 first item
[..] because the field or method was declared private, protected,
"package", or default access (not public), or because the class was
not declared public.
13.4.7
[..] Less access is permitted if the access modifier is changed from
"package" access to
private access; from protected access to "package" or privat access;
or from public access to protected, "package", or private access. [..]
14.3
It is a compile-time error if a local class declaration contains any
one of the following access modifiers: package, public, protected,
private, or static.
[* Is this correct? I guess, since no access modifier on local class
means local visibility not default visibility. ]
18.1
/Modifier:/
/Annotation/
package
public
protected
...
>> COMPATIBILITY:
>> A compiler switch is offered to set either strict or compatibility
>> mode. In strict mode, an error is issued if a member has not
>> visibility modifier.
>
> That would be a non-starter if mandatory; it would mean gratuitous
> source incompatibilities between JDK 7 and earlier source levels.
> An annotation processor can be written to do this to enforce local
> coding conventions.
Okay, dropped.
cheers,
AA
More information about the coin-dev
mailing list