Why are modifiers allowed for a package when they don't seem to do anything?
I can 开发者_运维知识库declare the following package with modifiers but it doesn't seem to have any effect on anything:
private public protected static final package com.stackoverflow.mangodrunk
// ...
class Whatever {
// ...
}
So my question is, does adding a modifier before a package decleration do anything and why would it be allowed by the compiler?
Update: Seems to be an issue with the compiler bundled with Eclipse, as others have mentioned this is a compiler error using Sun's JDK.
They're not. Which compiler are you using?
$ javac com/stackoverflow/mangodrunk/Whatever.java
com/stackoverflow/mangodrunk/Whatever.java:1: class, interface, or enum expected
private public protected static final package com.stackoverflow.mangodrunk;
^
1 error
According to the JLS, the only thing that can precede package
is an annotation.
PackageDeclaration: Annotationsopt package PackageName ;
But an annotation looks like this (full grammar omitted for brevity):
NormalAnnotation: @ TypeName ( ElementValuePairsopt )
So your compiler should not be allowing standard access modifiers.
Source: http://java.sun.com/docs/books/jls/third_edition/html/packages.html
精彩评论