1

I don't understand package modifiers (not annotations) like this:

public package foo;

public class Bar {
}

Do they have any meaning?

dotjpg3141
  • 305
  • 1
  • 10
  • There is no such thing as a package modifier in Java. – helpermethod Sep 03 '14 at 12:27
  • but it is a valid java syntax – dotjpg3141 Sep 03 '14 at 12:28
  • 3
    What do you mean by package modifier? "*but it is a valid java syntax*" no it is not, `public package foo;` is not valid in Java and will not compile. – Pshemo Sep 03 '14 at 12:29
  • Does this code compile with Oracle JDK? – Abimaran Kugathasan Sep 03 '14 at 12:29
  • No, it doesn't compile. I have never seen this ever being valid in any version of the java spec. – vikingsteve Sep 03 '14 at 12:31
  • It does compile and it's perfectly valid code. It shouldn't but it does. – biziclop Sep 03 '14 at 12:31
  • @biziclop : Do this compile with Oracle JDK? – Abimaran Kugathasan Sep 03 '14 at 12:34
  • 2
    @biziclop: If it shouldn't compile, then it's *not* perfectly valid code - and it definitely doesn't compile under all compilers. (It doesn't with Oralce's javac, for example.) – Jon Skeet Sep 03 '14 at 12:35
  • 1
    @JonSkeet I should've qualified it: it is recognised as valid code by Eclipse. It was just a reaction to the flurry of unwarranted downvotes. The question is perfectly reasonable, even though the answer to that may be "it's a bug in the Eclipse compiler". – biziclop Sep 03 '14 at 12:37
  • @biziclop I believe that downvotes ware correct reaction on unclear question. Since OP didn't describe why (s)he thinks that this code is valid we can't do anything else than point to specification fragment which explains that it is not (+1 for Jon for finding such fragment). It most probably is a bug in OPs IDE but we can't be sure which IDE is it, so for now -1 from me until this question will be [edit]ed to include a way to reproduce this behaviour/problem. – Pshemo Sep 03 '14 at 12:47

1 Answers1

7

The only type of package modifier in the JLS is an annotation. From JLS 7.4.1:

PackageDeclaration:
{PackageModifier} package Identifier {. Identifier} ;

PackageModifier:
Annotation

So public package foo; is invalid syntax.

This compiles under some versions of Eclipse, due to a bug which has been fixed in some conditions, and will be fixed in all conditions from Eclipse 4.4 onwards.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194